Jump to content

[SOLVED] Multiple Image Upload Problems


IronWarrior

Recommended Posts

Having yet more problems uploading multiple image files, so here goes, I have followed one of the tech-meal tutorials and it "should" work.

 

Code is as follows:

 

<?php 
if (isset($_POST["validTransaction"]))
{
	$vehicleName = $_POST["vehicleName"];
	$vehicleDescription = $_POST["vehicleDescription"];
	$vehiclePrice = $_POST["vehiclePrice"];
	$vehicleType = $_POST["vehicleType"];
	$vehicleCategory = $_POST["vehicleCategory"];
	$trimmedVehicleName = str_replace(" ","",$vehicleName);
	$folderLocation = "images/";
	$currentTime = time();

	//Checking if all fields are present 
	if ($vehicleName == "" || $vehicleDescription == "" || $vehiclePrice == "" || $vehicleType == "" || $vehicleCategory == "")
	{
		echo "Not all infomation has been completed, please use the return button to re-submit the infomation";
		$error = true;
	}
	//If all fields have been completed, then continue. 
	else 
	{
		//Adding the item details to the database
		mysql_query("INSERT INTO vehiclesforsale (vehicleName, vehicleDescription, vehiclePrice, vehicleType, vehicleCategory, timeAdded) 
					VALUES ('$vehicleName', '$vehicleDescription', '$vehiclePrice', '$vehicleType', '$vehicleCategory', '$currentTime')");
		$result = mysql_query("SELECT * FROM vehiclesforsale WHERE vehicleName='$vehicleName' AND timeAdded='$currentTime'");
		$row = mysql_fetch_array($result);
		$linkedItemID = $row['vehicleID'];
		// Multiple Image Loop
		print_r($_FILES);
		for ($index=0;$index<4;$index++)
		{
			$currentTime = $currentTime + $index;
			$newName = $folderLocation."/".$currentTime.".jpg";
			// Checking if an image has been uploaded
			if (is_uploaded_file($_FILES['imgfile']['tmp_name']['$index']))
			{
				//Copying the file to the directory
				if (!copy($_FILES['imgfile']['tmp_name']['$index'], $newName))
				{
					// Error message for file upload
					print "Error Uploading File.";
					exit();
				}
				//If the copy is sucessful
				else 
				{
					// Success message for file upload.
					echo "We have uploaded your file";
					//Adding the image infomation to the database
					mysql_query("INSERT INTO images (imageID, linkedItem, uploaderID) 
						VALUES ($currentTime, '$linkedItemID', '-')");
					// Creating the Small Image
						// The image to resize
						$sourceImage = imagecreatefromjpeg($newName);

						// Capture the original size of the uploaded image
						list($width,$height)=getimagesize($newName);

						// For our purposes, I have resized the image to be
						// 600 pixels wide, and maintain the original aspect
						// ratio. This prevents the image from being "stretched"
						// or "squashed". If you prefer some max width other than
						// 600, simply change the $newwidth variable
						$newwidth=300;
						$newheight=($height/$width)*300;
						$tmp=imagecreatetruecolor($newwidth,$newheight);

						// this line actually does the image resizing, copying from the original
						// image into the $tmp image
						imagecopyresampled($tmp,$sourceImage,0,0,0,0,$newwidth,$newheight,$width,$height);

						// now write the resized image to disk. I have assumed that you want the
						// resized, uploaded image file to reside in the ./images subdirectory.
						$newNameSmall =$folderLocation."/".$currentTime."small.jpg";
						imagejpeg($tmp,$newNameSmall,100);

						imagedestroy($sourceImage);
					// Creating the preview Image
						// The image to resize
						$sourceImage = imagecreatefromjpeg($newName);

						// Capture the original size of the uploaded image
						list($width,$height)=getimagesize($newName);

						// For our purposes, I have resized the image to be
						// 600 pixels wide, and maintain the original aspect
						// ratio. This prevents the image from being "stretched"
						// or "squashed". If you prefer some max width other than
						// 600, simply change the $newwidth variable
						$newwidth=210;
						$newheight=($height/$width)*210;
						$tmp=imagecreatetruecolor($newwidth,$newheight);

						// this line actually does the image resizing, copying from the original
						// image into the $tmp image
						imagecopyresampled($tmp,$sourceImage,0,0,0,0,$newwidth,$newheight,$width,$height);

						// now write the resized image to disk. I have assumed that you want the
						// resized, uploaded image file to reside in the ./images subdirectory.
						$newNameSmall =$folderLocation."/".$currentTime."preview.jpg";
						imagejpeg($tmp,$newNameSmall,100);

						imagedestroy($sourceImage);
				}
			}
		}
	}
}
else 
{
	echo "Invalid Transaction";
}
?>

 

The results of the print_r are:

 

Array ( [userfile] => Array ( [name] => Array ( [0] => 101_0439.jpg [1] => 101_0441.jpg [2] => 101_0442.jpg [3] => 101_0436.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg [2] => image/jpeg [3] => image/jpeg ) [tmp_name] => Array ( [0] => C:\Windows\Temp\php7D98.tmp [1] => C:\Windows\Temp\php7EB2.tmp [2] => C:\Windows\Temp\php8029.tmp [3] => C:\Windows\Temp\php8172.tmp ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 ) [size] => Array ( [0] => 1674938 [1] => 2089207 [2] => 1893763 [3] => 2070300 ) ) ) 

 

So we can see the array is working.... unsure as to what I have done wrong, Thanks in advance for assistance.

Link to comment
https://forums.phpfreaks.com/topic/121382-solved-multiple-image-upload-problems/
Share on other sites

Basically, no outputs what so ever, there are no errors and I cannot work out how to get it to give me an error message, anything like the mysql_error() function?

 

Edit: Also the files are not copied over to the image directory, simply put nothing is happening, unsure why as the $_FILES array is populated etc... the image re-sizing and copying worked with a single image, I have since adopted it to work with an array of images, this made it fail...

 

 

You can use error_reporting(E_ALL); at the top of your page. That's if there's errors. I haven't looked at your code properly. But I do see you have mysql_query() calls, I'd recommend you use the mysql_error() function with them.

 

mysql_query("SELECT * FROM table") or die("Error: ".mysql_error());

 

Just in case.

Basically $_FILES['imgfile']['tmp_name']['$index'] doesn't exist, and if you turn on error_reporting you should see this.

 

Note the use of single inverted commas ('). '$index' does not exist, it's looking for the array key '$index' instead of 0,1,2,3

however "$index" will exist, although still technically wrong because it's a string and you should be looking for an integer,

thus it should just be $index (no string encapsulation),

 

therefore, you should replace all areas where you see $_FILES['imgfile']['tmp_name']['$index']

with

$_FILES['imgfile']['tmp_name'][$index]

Sorry, should have remembered around literals from Java. So basically I have made the required modifications and we are still having issues, the code now reads:

 

<?php 
if (isset($_POST["validTransaction"]))
{
	$vehicleName = $_POST["vehicleName"];
	$vehicleDescription = $_POST["vehicleDescription"];
	$vehiclePrice = $_POST["vehiclePrice"];
	$vehicleType = $_POST["vehicleType"];
	$vehicleCategory = $_POST["vehicleCategory"];
	$trimmedVehicleName = str_replace(" ","",$vehicleName);
	$folderLocation = "images/";
	$currentTime = time();

	//Checking if all fields are present 
	if ($vehicleName == "" || $vehicleDescription == "" || $vehiclePrice == "" || $vehicleType == "" || $vehicleCategory == "")
	{
		echo "Not all infomation has been completed, please use the return button to re-submit the infomation";
		$error = true;
	}
	//If all fields have been completed, then continue. 
	else 
	{
		//Adding the item details to the database
		mysql_query("INSERT INTO vehiclesforsale (vehicleName, vehicleDescription, vehiclePrice, vehicleType, vehicleCategory, timeAdded) 
					VALUES ('$vehicleName', '$vehicleDescription', '$vehiclePrice', '$vehicleType', '$vehicleCategory', '$currentTime')");
		$result = mysql_query("SELECT * FROM vehiclesforsale WHERE vehicleName='$vehicleName' AND timeAdded='$currentTime'");
		$row = mysql_fetch_array($result);
		$linkedItemID = $row['vehicleID'];
		// Multiple Image Loop
		print_r($_FILES);
		for ($index=0;$index<4;$index++)
		{
			$currentTime = $currentTime + $index;
			$newName = $folderLocation."/".$currentTime.".jpg";
			// Checking if an image has been uploaded
			if (is_uploaded_file($_FILES['imgfile']['tmp_name'][$index]))
			{
				//Copying the file to the directory
				if (!copy($_FILES['imgfile']['tmp_name'][$index], $newName))
				{
					// Error message for file upload
					print "Error Uploading File.";
					exit();
				}
				//If the copy is sucessful
				else 
				{
					// Success message for file upload.
					echo "We have uploaded file number: $index";
					//Adding the image infomation to the database
					mysql_query("INSERT INTO images (imageID, linkedItem, uploaderID) 
						VALUES ($currentTime, '$linkedItemID', '-')");
					// Creating the Small Image
						// The image to resize
						$sourceImage = imagecreatefromjpeg($newName);

						// Capture the original size of the uploaded image
						list($width,$height)=getimagesize($newName);

						// For our purposes, I have resized the image to be
						// 600 pixels wide, and maintain the original aspect
						// ratio. This prevents the image from being "stretched"
						// or "squashed". If you prefer some max width other than
						// 600, simply change the $newwidth variable
						$newwidth=300;
						$newheight=($height/$width)*300;
						$tmp=imagecreatetruecolor($newwidth,$newheight);

						// this line actually does the image resizing, copying from the original
						// image into the $tmp image
						imagecopyresampled($tmp,$sourceImage,0,0,0,0,$newwidth,$newheight,$width,$height);

						// now write the resized image to disk. I have assumed that you want the
						// resized, uploaded image file to reside in the ./images subdirectory.
						$newNameSmall =$folderLocation."/".$currentTime."small.jpg";
						imagejpeg($tmp,$newNameSmall,100);

						imagedestroy($sourceImage);
					// Creating the preview Image
						// The image to resize
						$sourceImage = imagecreatefromjpeg($newName);

						// Capture the original size of the uploaded image
						list($width,$height)=getimagesize($newName);

						// For our purposes, I have resized the image to be
						// 600 pixels wide, and maintain the original aspect
						// ratio. This prevents the image from being "stretched"
						// or "squashed". If you prefer some max width other than
						// 600, simply change the $newwidth variable
						$newwidth=210;
						$newheight=($height/$width)*210;
						$tmp=imagecreatetruecolor($newwidth,$newheight);

						// this line actually does the image resizing, copying from the original
						// image into the $tmp image
						imagecopyresampled($tmp,$sourceImage,0,0,0,0,$newwidth,$newheight,$width,$height);

						// now write the resized image to disk. I have assumed that you want the
						// resized, uploaded image file to reside in the ./images subdirectory.
						$newNameSmall =$folderLocation."/".$currentTime."preview.jpg";
						imagejpeg($tmp,$newNameSmall,100);

						imagedestroy($sourceImage);
				}
			}
		}
	}
}
else 
{
	echo "Invalid Transaction";
}
?>

 

I have tried to break the code down to problem shoot, but its being a major problem...

 

Right have used the error report and got the following results:

 


Notice: Undefined index: imgfile in C:\AppServ\www\tanksforsale\submitAnItem.php on line 36

Notice: Undefined index: imgfile in C:\AppServ\www\tanksforsale\submitAnItem.php on line 36

Notice: Undefined index: imgfile in C:\AppServ\www\tanksforsale\submitAnItem.php on line 36

Notice: Undefined index: imgfile in C:\AppServ\www\tanksforsale\submitAnItem.php on line 36

 

Which is the line:

 

if (is_uploaded_file($_FILES['imgfile']['tmp_name'][$index]))

Array ( [userfile] => Array ( [name] => Array ( [0] => 101_0439.jpg [1] => 101_0441.jpg [2] => 101_0442.jpg [3] => 101_0436.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg [2] => image/jpeg [3] => image/jpeg ) [tmp_name] => Array ( [0] => C:\Windows\Temp\php7D98.tmp [1] => C:\Windows\Temp\php7EB2.tmp [2] => C:\Windows\Temp\php8029.tmp [3] => C:\Windows\Temp\php8172.tmp ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 ) [size] => Array ( [0] => 1674938 [1] => 2089207 [2] => 1893763 [3] => 2070300 ) ) ) 

 

Your array says that it's userfile, not imgfile.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.