Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    upload png showing as invalid

    I am using this script to upload files for testing, however it will not upload any png files. Always get an "Invalid Files" error

    http://www.w3schools.com/php/php_file_upload.asp


    PHP Code:
    $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"] < 20000)
     && 
    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("upload/" $_FILES["file"]["name"]))
           {
           echo 
    $_FILES["file"]["name"] . " already exists. ";
           }
         else
           {
           
    move_uploaded_file($_FILES["file"]["tmp_name"],
           
    "uploads/" $_FILES["file"]["name"]);
           echo 
    "Stored in: " "uploads/" $_FILES["file"]["name"];
           }
         }
       }
     else
       {
       echo 
    "Invalid file";
       } 
    Code:
    <form action="" method="post" enctype="multipart/form-data">
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file" /> 
    <br />
     <input type="submit" name="submitPhoto" value="Submit" />
     </form>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 538 Times in 525 Posts
    At the top of your code you should:
    var_dump($_FILES);

    Then look at the html source, copy it, paste it here in a reply.

    As for w3schools.. I don't know why they continue with their tutorials. They're useless yet because they offer a few fairly meaningless qualifications that look impressive on a CV, they are thought of as the 'bees knees' in the programming world.
    I can't really think of anything to write here now...

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Possibly because of
    Code:
    $_FILES["file"]["size"] < 20000
    in your code. You might be trying to upload an image having size >20kb
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    this is what I get with the var_dump($_FILES);

    array(1) { ["file"]=> array(5) { ["name"]=> string(8) "TEST.PNG" ["type"]=> string(11) "image/x-png" ["tmp_name"]=> string(38) "/home/content/01/6514501/tmp/phpLba12E" ["error"]=> int(0) ["size"]=> int(9091) } } Invalid file

    I know its not the size. The other image files load as they should at much larger sizes. But just to be sure I've removed the file size limit and it still won't work

    The thing is that particular code was working for all the image files and now its not just for the png images. Is there any other setting that could prevent them from loading?

    Looks like this is giving me the problem "image/x-png" if I add that to my code then it uploads the image

    PHP Code:
     || ($_FILES["file"]["type"] == "image/x-png"
    What is that about?

    Also do you know a good tutorial on uploading images, that is not W3school? and is simple?
    Last edited by harkly; 10-29-2012 at 03:27 PM.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 538 Times in 525 Posts
    Quote Originally Posted by harkly View Post
    Looks like this is giving me the problem "image/x-png" if I add that to my code then it uploads the image

    PHP Code:
     || ($_FILES["file"]["type"] == "image/x-png"
    What is that about?
    So stick with that then. It looks like the mime tye label is different for png than it is jpg / gif / bmp etc. No idea why but it looks like thats the reason here.

    Quote Originally Posted by harkly View Post
    Also do you know a good tutorial on uploading images, that is not W3school? and is simple?
    Google is full of them
    I can't really think of anything to write here now...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •