Search code examples
phphtmlajaxfile-uploadw3c

PHP image upload issue. Uploads images that are web optimised but not the original image


I am having this problem of uploading images using PHP. All of the images are JPEG

I have several files copied to my desktop from camera and iphone. All of the files are various size from 800Kb to 6MB.

Non of the above files that are from camera or iphone would upload to my server not because of MAX_UPLOAD_SIZE as I have already configured it to be 30M (30MB).

Now I opened two of these image (size 5.6MB and 1.9MB) in Photoshop and saved them for web images. Both of this image size stayed same (as I forced them to stay same). Now When I try to upload this photoshop optimised images onto my server, they upload successfully. I just can't understand what the problem is here. Are the images from iphone and camera has some restriction or a specific header that is restricting the upload? Please note that I copied the images to my desktop before uploading and not trying to upload straight from the device folder.

Here is the html and php coding of the file:

<?php
if (isset($_POST['submit'])){
    $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/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("image_test/" . $_FILES["file"]["name"]))
  {
    echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "image_test/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "image_test/" . $_FILES["file"]["name"];
  }
}
  }
  else
 {
   echo "Invalid file";
 }
 } 
?>

<html>
<body>

<form action="upload_test.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>

if someone has an answer or hint towards this problem then I would greatly appreciate it.

Kind regards


Solution

  • Found the answer, modified the top section of the above code as below:

    <?php
    if (isset($_POST['submit'])){
        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")))
    {
    

    Basically, it was due to end(). Tried to use the same script on localhost server whic started giving me an error as following:

    Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\search\php\loader\csvFileUploader1.php on line 9

    This lead me to research on it which gave me a solution that it is due to the end function in line 4.

    Thanks everyone for trying to help me. I do appreciate your comments.