-1

I am uploading an image in PHP.Image is of .jpg type but when uploading it, it is giving me error 0.

This is my code.

if(isset($_FILES["image_file"]["name"])){
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $extension = end(explode(".", $_FILES["image_file"]["name"]));
    if ((($_FILES["image_file"]["type"] == "image/gif")
    || ($_FILES["image_file"]["type"] == "image/jpeg")
    || ($_FILES["image_file"]["type"] == "image/jpg")
    || ($_FILES["image_file"]["type"] == "image/pjpeg")
    || ($_FILES["image_file"]["type"] == "image/x-png")
    || ($_FILES["image_file"]["type"] == "image/png"))
    //&& ($_FILES["image_file"]["size"] < $size)
    && in_array($extension, $allowedExts))
      {
      if ($_FILES["image_file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["image_file"]["error"] . "<br>";
        }
      else
        {
            $generate_random_number = rand(1,1000);
            $random_image_name = $generate_random_number.$_FILES["image_file"]["name"];
          if (file_exists($upload_dir_path.$random_image_name))
              {
                //echo $_FILES["file"]["name"] . " already exists. ";
              }
          else
          {
            move_uploaded_file($_FILES["image_file"]["tmp_name"],
            $upload_dir_path.$random_image_name);
          }
        }
      }
    else
      {print_r($_FILES);
        echo "Invalid file";
      }

}

When running the following is the output:

Array ( [image_file] => Array ( [name] => Chambers_block_study.JPG [type] => image/jpeg [tmp_name] => /tmp/phpghmfM2 [error] => 0 [size] => 2656165 ) )

Invalid file

Please help.Thanks!

5
  • That looks okay, doesn't it? Error 0 = no error.
    – Pekka
    Jul 8, 2013 at 7:16
  • What you're expecting ? Isn't it cool if your file is getting uploaded without any error ?
    – Rikesh
    Jul 8, 2013 at 7:16
  • I think Ravi is expecting that to output when there is an error, but it looks like the conditions are not correct so that will output when all is ok. Jul 8, 2013 at 7:18
  • @Rikesh file is not uploading. Jul 8, 2013 at 7:20
  • How do you know that, what happens? There clearly is a file in the tmp directory and it has a size.
    – Pekka
    Jul 8, 2013 at 7:20

3 Answers 3

1

Error 0 means no error, so everything should be okay. If you are having a problem, it is not caused by the file upload itself.

Side note: to detect a JPG image, better use getimagesize() instead of checking for extensions / MIME types. Those are set by the client and can be forged.

Example:

$info = getimagesize($_FILES["image_file"]["tmp_name"]);

if ($info == false) 
 {
 echo "Image is not a valid image file";
 // do whatever
 }
3
  • the main thing is not for checking type. error is coming so file is not uploading. Jul 8, 2013 at 7:21
  • I got the error. error was of case sensitive.I used array with jpg while file is of JPG so error was coming.Thanks Jul 8, 2013 at 7:25
  • @Ravi okay, but as said, that's not a very good method to check for whether it's an image.
    – Pekka
    Jul 8, 2013 at 7:34
1

maybe because it's case sensitive: you have jpg in your array, but files ends with JPG try

in_array(strtolower($extension), $allowedExts)
0
0

Straight from by the PHP manual:

UPLOAD_ERR_OK
Value: 0; There is no error, the file uploaded with success.

Which means your file was uploaded correctly.

If the user uploads a file with .JPG extension, your if statement will fail. So I'd rather use something like this for checking the image extension:

$image_ext =  strtolower(pathinfo($_FILES["image_file"]["name"],PATHINFO_EXTENSION));

Also, it's better to use getimagesize() for checking the validity of an image. You can use it as follows:

$isImageValid = @getimagesize($image_tmp);
if($isImageValid){
//do whatever
}

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.