Jump to content

[SOLVED] image.JPG cannot be uploaded - must be .jpeg or .jpg file?!


Guest

Recommended Posts

I have an image upload script that looks like this:

 

    $i = 1;
    while (!empty($_FILES['pic'.$i.'File']['name'])) {
      $imageSource = $_FILES['pic'.$i.'File']['tmp_name'];
      $imageType = $_FILES['pic'.$i.'File']['type'];
      $imageName = $_FILES['pic'.$i.'File']['name'];
      $imageDescription = $_POST['pic'.$i.'Description'];
      $imageAlbum = $_POST['pic'.$i.'Album'];
      $imageWeight = $_POST['pic'.$i.'Weight'];
      if ($imageType != 'image/jpeg' && $imageType != 'image/pjpeg') {
        echo "<p class=\"error\">Cannot upload ".stripslashes($imageName)." - new image must be a .jpeg or .jpg file!</p>";
      } else {
        $uniqueQuery = "SELECT name FROM images WHERE name = '$imageName' AND album = '$imageAlbum'";
        $uniqueResult = mysql_query($uniqueQuery) or die("Error in query: $uniqueQuery. ".mysql_error().".");
        if (mysql_num_rows($uniqueResult) != 0) {
          echo "<p class=\"error\">Cannot create ".stripslashes($imageName)." - image already exists!</p>";
        } else {
          $imagePic = addslashes(file_get_contents($imageSource));
          $imageCopy = imagecreatefromjpeg($imageSource);
          $imageWidth = imagesx($imageCopy);
          $imageHeight = imagesy($imageCopy);
          $thumbSource = 'tmp/'.substr($imageName, 0, -4).'Thumb.tmp';
          $thumbWidth = 100;
          $thumbHeight = 100;
          $imageThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
          imagecopyresampled($imageThumb, $imageCopy, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight);
          imagejpeg($imageThumb, $thumbSource, 100);
          $imageThumb = addslashes(file_get_contents($thumbSource));
          unlink($thumbSource);
          $insertQuery = "INSERT INTO images VALUES ('', 'image', '$imageName', '$imageAuthor', '$imageDatetime', '$imagePic', '$imageThumb', '$imageDescription', '$imageAlbum', '$imageWeight')";
          mysql_query($insertQuery) or die("Error in query: $insertQuery. ".mysql_error().".");
          echo "<p>Successfully uploaded ".stripslashes($imageName).".</p>";
        }
      }
      $i++;

 

It works fine for most images. However, my digital camera saves images as .JPG. The script doesn't seem to recognize these files as image/jpeg or image/pjpeg files. When I try renaming the file extension to .jpg, it still doesn't recognize it as the correct file type. I'm wondering if it has anything to do with the dpi, which is set to 180. Most .jpg files are 96. That's the only thing I can think of.

Try this

 

$i = 1;
    while (!empty($_FILES['pic'.$i.'File']['name'])) {
      $imageSource = $_FILES['pic'.$i.'File']['tmp_name'];
      $imageType = $_FILES['pic'.$i.'File']['type'];
      $imageName = $_FILES['pic'.$i.'File']['name'];
      $imageDescription = $_POST['pic'.$i.'Description'];
      $imageAlbum = $_POST['pic'.$i.'Album'];
      $imageWeight = $_POST['pic'.$i.'Weight'];
      
        $uniqueQuery = "SELECT name FROM images WHERE name = '$imageName' AND album = '$imageAlbum'";
        $uniqueResult = mysql_query($uniqueQuery) or die("Error in query: $uniqueQuery. ".mysql_error().".");
        if (mysql_num_rows($uniqueResult) != 0) {
          echo "<p class=\"error\">Cannot create ".stripslashes($imageName)." - image already exists!</p>";
        } else {
          $imagePic = addslashes(file_get_contents($imageSource));
          $imageCopy = imagecreatefromjpeg($imageSource);
          $imageWidth = imagesx($imageCopy);
          $imageHeight = imagesy($imageCopy);
          $thumbSource = 'tmp/'.substr($imageName, 0, -4).'Thumb.tmp';
          $thumbWidth = 100;
          $thumbHeight = 100;
          $imageThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
          imagecopyresampled($imageThumb, $imageCopy, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight);
          imagejpeg($imageThumb, $thumbSource, 100);
          $imageThumb = addslashes(file_get_contents($thumbSource));
          unlink($thumbSource);
          $insertQuery = "INSERT INTO images VALUES ('', 'image', '$imageName', '$imageAuthor', '$imageDatetime', '$imagePic', '$imageThumb', '$imageDescription', '$imageAlbum', '$imageWeight')";
          mysql_query($insertQuery) or die("Error in query: $insertQuery. ".mysql_error().".");
          echo "<p>Successfully uploaded ".stripslashes($imageName).".</p>";
        }
      }
      $i++;

 

Have you actually spit out the resulting $_FILES value to the screen for one of these images? Just for kicks, place this at the top of your script and see what the output is. I bet one of the items you're checking has an unexpected value in it:

<?php
echo "<pre>" . print_r($_FILES, TRUE) . "</pre>";
?>

Array
(
    [pic1File] => Array
        (
            [name] => 2007 09 29 2.JPG
            [type] => 
            [tmp_name] => 
            [error] => 2
            [size] => 0
        )

    [pic2File] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [pic3File] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [pic4File] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [pic5File] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

 

Hmm, interesting. It has no type?

Hmm, interesting. It has no type?

 

Very interesting. I have had times where the $_FILES array was not reliable to get the type, and I actually ran a command line call on the temp file to get the MIME type instead... maybe something like that would better suit your need.

Code must always check for any possible errors that could occur before blindly accessing data that could be nonexistent (hackers trigger error messages to learn file system paths and account usernames... by feeding scripts invalid data.)

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.