Page 1 of 1

Upload script only partially working

Posted: Wed Aug 02, 2006 11:58 am
by pvanlaer
I have put together a php script that's supposed to:
  • check whether the files uploaded are jpg's
  • rename all uploaded jpg's using this naming convention "$filename = uniqid("")."_1".".".$extention;" resulting in a filename like 44d0d32a83aaa_1.jpg
  • create thumbnails for each jpg and store them using the same filename but prefixed with "tb_" resulting in a filename like tb_44d0d32a83aaa_1.jpg
  • save the filename into the database with a unique id
The script does all the above but for some mysterious reason only handles the first 6 images of each post, regardless of how many are posted. Any image past the first 6 just disappears without any error.
At first I thought it was the memory limit or the post max size, but raising them to 30M did not help. Then I wrote a script to handle multiple uploads without resizing & database interaction. With this script I was able to:
  • successfully upload a 15M file
  • successfully upload 10+ images
I can't seem to come up with a solution
I hope some of you see where the problem resides, or post me some cleaner code that does about the same.

thx in advance
Peter

here's the code:

Code: Select all

<?php
	// database configuration & variable "$images_dir"
	include("config.inc.php");

	// initialization
	$result_final = "";
	$counter = 0;

	// List of our known photo types
	$known_photo_types = array( 
						'image/pjpeg' => 'jpg',
						'image/jpeg' => 'jpg',						
					);
	
	// GD Function List
	$gd_function_suffix = array( 
						'image/pjpeg' => 'JPEG',
						'image/jpeg' => 'JPEG',						
					);

	// Fetch the photo array sent by preupload.php
	$photos_uploaded = $_FILES['userfile'];

	// Fetch the photo caption array
	$photo_caption = $_POST['photo_caption'];

	while( $counter <= count($photos_uploaded) )
	{
		if($photos_uploaded['size'][$counter] > 0)
		{
			if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
			{
				$result_final .= "File ".($counter+1)." is not a photo<br />";
			}
			else
			{
				mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );
				$new_id = mysql_insert_id();
				$filetype = $photos_uploaded['type'][$counter];
				$extention = $known_photo_types[$filetype];
				$filename = uniqid("")."_1".".".$extention;

				mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );

				// Store the orignal file
				copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);

				// Let's get the Thumbnail size
				$size = GetImageSize( $images_dir."/".$filename );
				if($size[0] > $size[1])
				{
					$thumbnail_width = 250;
					$thumbnail_height = (int)(250 * $size[1] / $size[0]);
				}
				else
				{
					$thumbnail_width = (int)(250 * $size[0] / $size[1]);
					$thumbnail_height = 250;
				}
			
				// Build Thumbnail with GD 2.x.x, you can use the other described methods too
				$function_suffix = $gd_function_suffix[$filetype];
				$function_to_read = 'ImageCreateFrom' . $function_suffix;
				$function_to_write = 'Image' . $function_suffix;
				
				// Read the source file
				$source_handle = $function_to_read($images_dir . '/' . $filename);
					   
				if ($source_handle) {
				 // Let's create a blank image for the thumbnail
				 $destination_handle =
				   ImageCreateTrueColor($thumbnail_width, $thumbnail_height);
				
				 // Now we resize it
				 ImageCopyResampled($destination_handle, $source_handle,
				   0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);
				}
				
				// Let's save the thumbnail
				$function_to_write($destination_handle, $images_dir . '/tb_' . $filename);
								//

				$result_final .= "<img src='"."http://www.xxxxxxxxxxxx.be/applet/uploads". "/tb_".$filename."' /> File ".($counter+1)." Added<br />";
			}
		}
	$counter++;
	}

	// Print Result
echo <<<__HTML_END

<html>
<head>
	<title>Photos uploaded</title>
</head>
<body>
	$result_final
</body>
</html>

__HTML_END;
?>

nobody has a solution?

Posted: Wed Aug 02, 2006 2:19 pm
by pvanlaer
nobody has a solution or a clue as to what is causing this problem?

Posted: Wed Aug 02, 2006 2:21 pm
by Luke
psst... that's bumping! (mods will slap you) but anyway... I'll take a look at it... gimme a minute.

Post your html page too please.

don't use html form for post

Posted: Wed Aug 02, 2006 2:39 pm
by pvanlaer
I don't use a html form. The upload is done true a java applet that resizes images before uploading them.

You can test it live over here:
http://aecsmt.be/applet/applet.php
try to upload more than 6 images you'll see only 6 are saved.


Here you can download a rar of all files as they are on the server right now.
http://aecsmt.be/applet/upload.rar


ps. If it would be to much work to get it right pm me and i'll see if I can pay you for it.[/url]

ps. sorry for the bumping won't happen again! It's just that I'm so desperate to getting this to work.

Posted: Thu Aug 03, 2006 3:36 pm
by Luke
Very strange... I'm unable to figure out what's going on with it. I know very little about java and java applets thought... could it be a problem with the applet?

Re: Upload script only partially working

Posted: Thu Aug 03, 2006 4:30 pm
by volka
pvanlaer wrote:Then I wrote a script to handle multiple uploads without resizing & database interaction. With this script I was able to:
  • successfully upload a 15M file
  • successfully upload 10+ images
Maybe it's a timeout problem. Did you also increase the value of max_execution_time?

I've found it!!!:)

Posted: Thu Aug 03, 2006 5:57 pm
by pvanlaer
It was a problem with the variable name, in wich the java applet posts the name of the uploaded images in the $_FILES array.

First I correctly declared this

Code: Select all

$photos_uploaded = $_FILES['userfile'];
But in order to get it to work, I had to change the while statement from this

Code: Select all

while( $counter <= count($photos_uploaded) )
to this

Code: Select all

while( $counter <= count($photos_uploaded['name']) )
It's late over here and I'm tired, I can finaly go to sleep now :)
Thx for al replies :wink: