Very odd issue with file upload script

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?

You can check the files array; some jpg are shown as pjpeg ? or something so you would need to accept “image/pjpeg” as well.

The errors are here : $_FILES[photos][error] you will need to check the php manual for explinations of file uploads but if you add this to your code you should see all the form information:

print_r( $_POST ); echo '<pre>'; print_r( $_FILES ); echo '</pre>'; 

print_r( $_FILES ); Part should show you the file types and the errors are [4]

Thanks, well this is the output I get:

Array ( [submit] => Submit )

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

It doesn’t seem to give any pointers as to what the problem may be?

Very sorry I didn’t read accross your code properly - you have more than one type.

The file upload explanations etc. can be found here:

This is what I get for a successful file upload:

Array ( [submit] => Submit )

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

It doesn’t look any different really- aside from the file name of course…

Just been out for dinner and back at 21:15 - showing my age :wink:

It looks like the file was uploaded OK. I would put some more echos in to try and locate where the problem is e.g. Is $url or $copy what you expect it to be.

Looking at the flow the image is uploaded to the Temp file and copied to the new location - is that in the new folder - if not it must be failing at the copy point.
The thumbnail is then created - is it actualy in the folder and the filename or something is incorrect ?

I do not know why you need to count the colours etc.

Once the file is in the $_FILES array do a var_dump() on the files array:


Do the values look like what you was expecting, ie file size, MIME, etc.