How to use the $_FILES superglobal to check a file is any image type.
Results 1 to 8 of 8

Thread: 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
    238

    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,801
    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 Contact Unit "Coping Mechanism"
    Posts
    22,488
    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
    238
    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 Contact Unit "Coping Mechanism"
    Posts
    22,488
    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
    238
    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,801
    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
    238
    Thanks man.

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
  •