Results 1 to 9 of 9

Thread: When exactly does getimagesize return false?

  1. #1
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default When exactly does getimagesize return false?

    I have getimagesize returning false after an upload. I used set_error_handler('errors', -1) to ensure no error is being thrown to help me know the problem. As far as I can ascertain, the PHP manual doesn't specify situations where false is returned but no errors are thrown. Can anyone shed some light for me?

    More generally, is getimagesize even a good function for checking the success of an image upload? I have a bug I'm trying to fix in code written by someone else, involving this function.

    Thanks for any advice.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Just put the move file function in a conditional or post your code and we can provide examples.
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    The algorithm is based on this, but the code I'm asking about was added to that:
    PHP Code:
          if(getimagesize($_FILES['upload']['tmp_name']) == FALSE){
            if(isset(
    $_GET['base64'])){ /* The former developer suspected Firefox of inappropriately sending the images as base64 thereby triggering the outer conditional. */
              
    echo "TESTING";
            }else{
              
    $firephp->log(file_exists($_FILES['upload']['tmp_name'])); // 1
              
    echo "error"// JavaScript interprets this as a failed upload.
              
    die();
            }
          } 
    I hope that's enough information, as a minimal test case would be difficult to make.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    getimagesize() is overkill if all you're trying to do is confirm an upload. If you also need to make sure the image is a particular format, dimension, etc. etc., then it's perfect.

    Either way, I would confirm that the file was uploaded before using getimagesize(). That way, you can avoid the processing overhead if there's no file.

    To answer you original question, getimagesize() returns false "on failure." that could mean that the file didn't exist, was unreadable (e.g., insufficient permissions), was not a supported image format, or some other problem.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Do you need to check whether it's a valid image or just that a file exists? As traq said, it's overkill to use this if you don't need to verify it, but you do need to verify it, then I could post some code that might help.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I've confirmed the previous developer's hunch that Firefox is sometimes sending images in base 64, and that's (by mechanics I don't understand) what triggers the false result from getimagesize. I think other cases are accounted for by this particular check (and not code that executes before it), so...

    Yes, I'll be using the above or similar code to validate the file. Currently I have the base 64 check inside this one (using a regex to check for a base64 MIME type in the image data), although I might should move it up in the source?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's weird. I've never encountered Base 64 for uploads. It looks like the script you linked to is probably right since I've never dealt with that type of upload, but I'm not sure how to help.
    Of course base64_decode() is available... would that help at all?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I've never heard of (or had problems with) Firefox uploading images using base64 either.

  9. #9
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Sorry I took so long to get back. I'm posting the modified PHP for review and reuse.

    In Firefox and other browsers that support sendAsBinary:
    Code:
    $ext = strtolower(getExtension($_FILES['upload']['name']));
    $theFileName = strtolower($_FILES['upload']['name']);
    $size = filesize($_FILES['upload']['tmp_name']);
    if ($size > config::max_size * 1024){
      echo "Error: ".$theFileName." is too big";
      die();
    }
    if (($ext != "jpg") && ($ext != "jpeg") && ($ext != "png") && ($ext != "gif")){
      echo "Error: ".$theFileName." is not a supported file type";
      die();
    }
    if(getimagesize($_FILES['upload']['tmp_name']) == FALSE){
      if(isset($_GET['base64'])){
        echo "TESTING";
      }else if(0 < preg_match('/^data:image\/.+?;base64,/', file_get_contents($_FILES['upload']['tmp_name']))){
        $contents = file_get_contents($_FILES['upload']['tmp_name']);
        file_put_contents($_FILES['upload']['tmp_name'], base64_decode(preg_replace('/^data:image\/.+?;base64,/', '', $contents)));
        $size = filesize($_FILES['upload']['tmp_name']);
      }else{
        echo "Error: Unknown";
        die();
      }
    }
    and for Chrome (which always sends in base64 but, I've discovered, sometimes prefixes the data with the MIME type) and any other browsers that put the image data in php://input rather than $_FILES:
    Code:
    if(isset($_GET['base64'])) {
      $content = file_get_contents('php://input');
      if(0 < preg_match('/^data:image\/.+?;base64,/', $content, $matches)){
        $content = preg_replace('/^data:image\/.+?;base64,/', '', $content);
      }
      $content = base64_decode($content);
    } else { // not sure this can ever happen...
      $content = file_get_contents('php://input');
      if(0 < preg_match('/^data:image\/.+?;base64,/', $content, $matches)){
        $content = base64_decode(preg_replace('/^data:image\/.+?;base64,/', '', $content));
      }
    }
    $headers = getallheaders(); // UP-* headers are sent by JavaScript
    $headers = array_change_key_case($headers, CASE_UPPER); //different case was being used for different browsers
    $ext = strtolower(getExtension($headers['UP-FILENAME']));
    $size = $headers['UP-SIZE'];
    
    if (($ext != "jpg") && ($ext != "jpeg") && ($ext != "png") && ($ext != "gif")){
      echo "Error: ".$theFileName." is too big";
      die();
    }
    if ($size > config::max_size * 1024){
      echo "Error: ".$theFileName." is not a supported file type";
      die();
    }
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

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