View Full Version : Very odd issue with file upload script

01-11-2010, 06:46 PM
I have a strange issue with a PHP image upload script that I'm using to place photos in a specific directory on a site.

Basically, it works for a few images but not most of them. The script only accepts JPG files but beyond that there's no reason why it wouldn't accept some JPGs and not others.

The code is as follows:

$idir = "../images/carpictures/"; // Path To Images Directory
$tdir = "../images/carpictures/"; // Path To Thumbnails Directory
$twidth = "125"; // Maximum Width For Thumbnail Images
$theight = "100"; // Maximum Height For Thumbnail Images

if (!isset($_GET['subpage'])) { // Image Upload Form Below ?>
<form method="post" action="addnewphotos.php?subpage=upload" enctype="multipart/form-data">
File:<br />
<input type="file" name="imagefile" class="form">
<br /><br />
<input name="submit" type="submit" value="Submit" class="form"> <input type="reset" value="Clear" class="form">
<? } else if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
$url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print '<p>Image uploaded successfully.</p><br />'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print '<p>Image created successfully.</p><p><a href="addnewphotos2.php">Upload another image</a></p><p><a href="admincomplete.php">Click here if finished</a></p>'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
} ?>

The script still appears to work (i.e doesn't throw an error) even on the occasions when it fails to upload the JPG file.

With two files found so far it actually works- with the others the file that it tries to upload doesn't appear on the server.

The permissions of all these files are the same. I'm uploading them from a Windows box to a Linux server.

The script has no particular file size limit, and all the files I'm trying to upload are roughly the same size (about 50K).

I've checked other attributes such as (a) making sure the images are all the same image format i.e RGB, and (b) checking to see if the script doesn't like particular resolutions- but there's no rhyme or reason to it.

This one has me stumped- any ideas?

01-11-2010, 07:14 PM
looking at your upload code only*, other then this being a very messy script, I don't see any reason why it would fail. though why do you use copy() instead of move_uploaded_file() ?

01-11-2010, 07:40 PM
The original script used copy, and it seems to work (for some images anyway).

I've also tried outputting the array in the script and got the following output for a successful upload:

Array ( [submit] => Submit )

Array ( [imagefile] => Array ( [name] => k3.jpg [type] => image/jpeg [tmp_name] => /tmp/phpxfJ628 [error] => 0 [size] => 58836 ) )

For an unsuccessful upload: (pretty much the same)

Array ( [submit] => Submit )

Array ( [imagefile] => Array ( [name] => k1.jpg [type] => image/jpeg [tmp_name] => /tmp/php0d83Zq [error] => 0 [size] => 70222 ) )

So still no idea why it's not working...