[RESOLVED] How to use the $_FILES superglobal to check a file is any image type.
Results 1 to 9 of 9

Thread: [RESOLVED] How to use the $_FILES superglobal to check a file is any image type.

  1. #1
    Senior Member Paul help!'s Avatar
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    251

    resolved [RESOLVED] How to use the $_FILES superglobal to check a file is any image type.

    Hi guys. I wanted to find out how to check whether a file is ANY image type, using the $_FILES superglobal?

    As opposed to something like this:

    $_FILES["photo"]["type"] == "image/jpeg";
    ... which only check one image type.

    If there one quick way of checking ALL image types?

    Paul.

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,886
    One way is to run it through the getimagesize() function. If the result is empty, then it's not an image -- or at least not of a type that your PHP installation recognizes.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,543
    It's strange that there is a caution on the getimagesize page that it could return false positives on certain files, and that the Fileinfo extension should be used instead. After all, Fileinfo is just as vulnerable to false positives, possibly even moreso. I can see it making a performance difference for really large files, but I'm not seeing the robustness argument.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Senior Member Paul help!'s Avatar
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    251
    thanks guys.

    Can you give me some example code of how I would use getimagesize() to achieve my original question (above).

    Paul.

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,543
    getimagesize, Example #1. Ignore the bit about $fp because you're not looking to read the file back out again.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Senior Member Paul help!'s Avatar
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    251
    Thanks.

    I have tried using it and I keep getting this error message:

    Warning: getimagesize(image/jpeg): failed to open stream: No such file or directory in C:\wamp\www....
    And below is the code where I have used it:

    PHP Code:
    if( isset($_FILES["articleimg"]) && $_FILES["articleimg"]["error"] == UPLOAD_ERR_OK ){
    if( 
    $_FILES["articleimg"]["size"] > 1000000 ){
    echo 
    "Image file is to big. Please reduce file size and re-upload. Image size must be under 1 megabyte in size.";
    } elseif( !
    getimagesize($_FILES["articleimg"]["type"]) ){
    echo 
    "The file type must be an image! Please upload an image file.";
    } elseif (!
    move_uploaded_file$_FILES["articleimg"]["tmp_name"], "images/" basename$_FILES["articleimg"]["name"] ) )){
    echo 
    "Cannot upload image!";
    }

    } else{
    switch( 
    $_FILES["articleimg"]["error"] ){
    case 
    UPLOAD_ERR_NO_FILE:
    $message "Please choose and image file!";
    break;
    default:
    $message "Cannot upload photo. Please contact your web designer.";
    }
    echo 
    $message;

    .......... to be more specific:

    PHP Code:
    } elseif( !getimagesize($_FILES["articleimg"]["type"]) ){ 

    Where am I going wrong?

  7. #7
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,886
    Do the check on the 'tmp_name' element of the array, not the 'type' element -- it needs to read the actual file.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  8. #8
    Senior Member Paul help!'s Avatar
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    251
    Thanks man.

  9. #9
    Senior Member
    Join Date
    Apr 2003
    Location
    Flanders Fields
    Posts
    5,833
    If I remember correctly, $_FILES["photo"]["type"] is reported by the remote user's browser (or via headers) and IS NOT TO BE TRUSTED because it could be spoofed. They could say "yeah buddy this is a jpeg" where in fact it might be a virus or something.

    getimagesize is a function that parses an image file to tell you stuff about it. If you call this function on a file that is not an image, it should return false:
    PHP Code:
    $size getimagesize("/tmp/foo.php"); // this file is a PHP file, not an image
    var_dump($size); 
    the output:
    Code:
    bool(false)
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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