slevin
slevin

Reputation: 3896

upload images with php, some images dont upload

I'm implementing a code to upload/save images to the server using PHP.

Some images get uploaded and saved, just fine. Those images are photos I have downloaded from the internet or pictures someone else gave me.

But the photos I took with my own digital camera, do not get uploaded, I get "Invalid file". Now, my photos are .JPG. I changed the code to also include the JPG string. I checked and edit the upload_max_filesize on the php.ini, so its not the size.

I use PHP 5.3.13

Any ideas? What kind of sorcery is this?

EDIT

Maybe its the metadata of my pictures? The settings from the digital camera? Is there a workaround?

Thanks in advance

Here is the code of the server side

$allowedExts = array("gif", "jpeg", "jpg", "png");
 $temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
 if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/x-png")
 || ($_FILES["file"]["type"] == "image/png"))
 && ($_FILES["file"]["size"] < 4000000)
 && in_array($extension, $allowedExts))
   {
   if ($_FILES["file"]["error"] > 0)
     {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
     }
   else
     {
     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
     echo "Type: " . $_FILES["file"]["type"] . "<br>";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

     if (file_exists("cultmapsite/upload" . $_FILES["file"]["name"]))
       {
       echo $_FILES["file"]["name"] . " already exists. ";
       }
     else
       {
       move_uploaded_file($_FILES["file"]["tmp_name"],
       "upload/" . $_FILES["file"]["name"]);
       echo "Stored in: " . "cultmapsite/upload" . $_FILES["file"]["name"];
       }
     }
   }
 else
   {
   echo "Invalid file";
   }

Upvotes: 0

Views: 820

Answers (4)

BjornBogers
BjornBogers

Reputation: 108

Try to convert the images from your digital camera to another extention like .png

Upvotes: 0

anilyeni
anilyeni

Reputation: 774

instead of echoing "Invalid file",

echo "Invalid File : " . $_FILES["file"]["name"] . $_FILES["file"]["type"];

and you will see why it is returning that.

Upvotes: 0

Nick Zulu
Nick Zulu

Reputation: 331

you changed your code to include the ($_FILES["file"]["type"] == "image/JPG") but since you are also using explode and the $allowedExts array you should also include JPG there...

try

$allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG");

Upvotes: 2

user2320325
user2320325

Reputation: 61

Try .jpg in place of .JPG I think it may be uppercase letter problem

Upvotes: 0

Related Questions