Hi
Extreme Newbie here.
I need some with my upload script. I am using allowing users to upload up to 8 photos. These are written to their userid directory. However i need to change the filename to something like photo1, photo2, photo3, etc. I think i need to use $key to access their position in the array for the number. Each photo is associated with a text description, so i need to know these photos are stored with a specific name to associate with the text description. I have tried to get this to work but cannot work it out. I have posted my code below, and would appraciate anybodys time to help me out. Note. the example below only has two uploads but there will be 8, and doesnt have the description text field in it.
Many thanks in advance
<?php require_once('Connections/connadmin.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_get_property_id = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_get_property_id = $_SESSION['MM_Username'];
}
mysql_select_db($database_connadmin, $connadmin);
$query_get_property_id = sprintf("SELECT property_id FROM master_property_reference WHERE username = %s", GetSQLValueString($colname_get_property_id, "text"));
$get_property_id = mysql_query($query_get_property_id, $connadmin) or die(mysql_error());
$row_get_property_id = mysql_fetch_assoc($get_property_id);
$totalRows_get_property_id = mysql_num_rows($get_property_id);
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 51200);
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/wamp/www/images/');
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
foreach ($_FILES['image']['name'] as $number => $file) {
// replace any spaces in the filename with underscores
$file = str_replace(' ', '_', $file);
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of an permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type'][$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error'][$number]) {
case 0:
// $username would normally come from a session variable
$propertyid = $_POST['property_id'];
// if the user's subfolder doesn't exist yet, create it
if (!is_dir(UPLOAD_DIR.$propertyid)) {
mkdir(UPLOAD_DIR.$propertyid);
}
// check if a file of the same name has been uploaded
if (!file_exists(UPLOAD_DIR.$propertyid."/".$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$propertyid.'/'.$file);
}
else {
// get the date and time
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$now.$file);
}
if ($success) {
$result[] = "$file uploaded successfully";
}
else {
$result[] = "Error uploading $file. Please try again.";
}
break;
case 3:
$result[] = "Error uploading $file. Please try again.";
default:
$result[] = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'][$number] == 4) {
$result[] = 'No file selected';
}
else {
$result[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Multiple file upload</title>
</head>
<body>
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo '<ol>';
foreach ($result as $item) {
echo "<strong><li>$item</li></strong>";
}
echo '</ol>';
}
?>
<form action="" method="post" enctype="multipart/form-data" name="multiUpload" id="multiUpload">
<p>
<label for="image1">File 1:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input name="username" type="hidden" value="<?php echo $_SESSION['MM_Username']; ?>" />
<input name="property_id" type="hidden" value="<?php echo $row_get_property_id['property_id']; ?>" />
<input type="file" name="image[]" id="image1" />
</p>
<p>
<label for="image2">File 2:</label>
<input type="file" name="image[]" id="image2" />
</p>
<p>
<input name="upload" type="submit" id="upload" value="Upload files" />
</p>
</form>
</body>
</html>
<?php
mysql_free_result($get_property_id);
?>
multi file upload target filename problem
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: multi file upload target filename problem
That's a lot of code, can you post the most relevant parts?
Also, try to use the code tags (Code button in editor) when posting code.
Also, try to use the code tags (Code button in editor) when posting code.
Re: multi file upload target filename problem
sure sorry. Not sure what you mean by code tags ?
Anyway the code i posted does not have my attempt to fix this problem i am stumped on where i use $key within the script, very new to all this. I have found a few examples but cannot get them to work. Many thanks in advance for your help
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/wamp/www/images/');
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
foreach ($_FILES['image']['name'] as $number => $file) {
// replace any spaces in the filename with underscores
$file = str_replace(' ', '_', $file);
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of an permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type'][$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error'][$number]) {
case 0:
// $username would normally come from a session variable
$propertyid = $_POST['property_id'];
// if the user's subfolder doesn't exist yet, create it
if (!is_dir(UPLOAD_DIR.$propertyid)) {
mkdir(UPLOAD_DIR.$propertyid);
}
// check if a file of the same name has been uploaded
if (!file_exists(UPLOAD_DIR.$propertyid."/".$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$propertyid.'/'.$file);
}
else {
// get the date and time
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$now.$file);
}
if ($success) {
$result[] = "$file uploaded successfully";
}
else {
$result[] = "Error uploading $file. Please try again.";
}
break;
Anyway the code i posted does not have my attempt to fix this problem i am stumped on where i use $key within the script, very new to all this. I have found a few examples but cannot get them to work. Many thanks in advance for your help
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/wamp/www/images/');
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
foreach ($_FILES['image']['name'] as $number => $file) {
// replace any spaces in the filename with underscores
$file = str_replace(' ', '_', $file);
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of an permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type'][$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error'][$number]) {
case 0:
// $username would normally come from a session variable
$propertyid = $_POST['property_id'];
// if the user's subfolder doesn't exist yet, create it
if (!is_dir(UPLOAD_DIR.$propertyid)) {
mkdir(UPLOAD_DIR.$propertyid);
}
// check if a file of the same name has been uploaded
if (!file_exists(UPLOAD_DIR.$propertyid."/".$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$propertyid.'/'.$file);
}
else {
// get the date and time
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$now.$file);
}
if ($success) {
$result[] = "$file uploaded successfully";
}
else {
$result[] = "Error uploading $file. Please try again.";
}
break;