SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image upload - verify file type help

    Hi guys,

    Im having trouble getting this to work, i want to validate that the uploaded image ($picture) is a gif or jpeg.

    PHP Code:
    // Validate the image type. Should be jpeg, jpg, or gif. 
            
    $allowed = array ('image/gif''image/jpeg''image/jpg'); 
            if (
    in_array($_FILES['picture']['type'], $allowed)) 
            { 
            
    $errors['picture2'] = 'Please ensure the image is a JPEG or GIF.';   
        } 
    Can anyone possibly help?

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From php.net:
    $_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
    This should be better then:
    Code PHP:
    function file_extension($filename) {
      return strtolower(end(explode(".", $filename)));
    }
     
    $allowed = array('gif', 'jpg', 'jpeg', 'png');
    if (!in_array(file_extension($_FILES['Picture']['name']), $allowed) {
        // Filetype not allowed
    }
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi hexburner

    Thank you!!!, that did the trick

    One last question...
    Do you know a better routine to check the dimensions of an image?

    This one wont validate true for a 120wide x 90high image?

    PHP Code:

    // Get image width and height and mime type
        
    $image getimagesize("$picture");
        
    $type $image['mime'];
        
    $width $image[0];
        
    $height $image[1];

    // Validate the pictures dimensions (length & width set in config file)
    if(($image[0] > $max_width) || ($image[1] >$max_height)) {
    $errors['picturedimensions'] = "Your image must not exceed "$max_height " x "$max_width .".";     
        } 

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I would use getimagesize to verify that the image is an image and the size.

    Code php:
    // Validate the image type. Should be jpeg, jpg, or gif. 
    $allowed = array ('image/gif', 'image/jpeg', 'image/jpg'); 
    if (in_array($_FILES['picture']['type'], $allowed)) { 
        $errors['picture2'] = 'Please ensure the image is a JPEG or GIF.';
    }

    Would be replace with

    Code php:
    if (($gi = getimagesize($_FILES['picture']['tmp_name'])) === false) {
        // Not an image exit...
    } else {
        list($width, $height, $type) = $gi;
     
        $allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPG, IMAGETYPE_PNG);
        if (!in_array($type, $allowed)) {
            // Bad File...
        }
     
        if (($width > $max_width && $height > $max_height)) {
            // Bad Width...
        }
    }

    Not tested but should be something like that. If the file is not an image it will error out.

    Checking the extension is not enough to verify it is indeed what it is.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you mean? Your code seems to be working fine. Could you please tell us that in which condition the image 120x90 could not be validated?

    I don't think that there is another such way of finding the image dimensions.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rajug,

    I just created 2 different images 120wide x 90high, the script wont let me submit but it will accept an image of a much smaller size??

    Maybe my settings in the config file causing the error?
    PHP Code:
    $max_height 120;
    $max_width 90
    Or is the settings of image width and height wrong?

  7. #7
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just saw it!!
    LOL sorry for the silly mistake guys

    $max_height = 120;
    $max_width = 90;


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
  •