Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I've got a php image uploader that works fine until you try and upload a jpg image that has the extension in capitals; i.e. works.jpg is okay but doesntwork.JPG isn't.

Here's the code (W3C jobbie);
PHP
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 5000000)
&& 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("../gallery/photos/All/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../gallery/photos/All/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../gallery/photos/All/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

As you see it's has a5mb size limit (well 4.7mb something), the file being uploaded isn't over this limit.

Any help would be greatly appreciated.

Thanks,
James
Posted
Comments
enhzflep 5-Jul-13 17:44pm    
What happened when you tried adding "JPEG" to the array on line 2 of your code?

I.e I expect you'd find GIF PNG and JPEG would all fail too, since it's only lower-case extensions that seem to be checked.

it was suppose to do that.
convert the extension into lower case your problem will be solved.

i.e.
PHP
$extension =strtolower($extension);
 
Share this answer
 
Try this...:)

PHP
$allowedExts = array("jpg", "jpeg", "gif", "png","JPG","JPEG");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/JPEG")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/pjpeg"))

&& 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("../gallery/photos/All/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../gallery/photos/All/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../gallery/photos/All/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
</br></br></br></br></br>


HTML
Now you can upload image having size 5mb also and having extension JPG OR JPEG
 
Share this answer
 
Comments
J3ffers 9-Jul-13 16:30pm    
I did try this originally and it wouldn't have it, seems the iPhone saves images a .JPG so I had issues uploaded images from the iPhone.. works perfect now thanks, I'm still unsure of what I missed when I tried this approach.

Thanks,
James

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900