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 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    checking file types

    I have files grabbed from a directory...

    image.jpg image.gif image.png

    How would I check if the file is a certain type

    if ($value != ".png"){
    echo $value;
    }

    Thanks for any help.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Do you mean just an extension?
    You can use the pathinfo function to do this.
    PHP Code:
    echo pathinfo($valuePATHINFO_EXTENSION); 
    Or you can just use strpos with substr:
    PHP Code:
    if(false !== ($pos strrpos($value'.')))
    {
        
    $ext substr($value$pos 1);

    Note that you can use strpos as well with the entire extension. If its not false, it contains it, but it doesn't verify that its the extension.
    Also note that the extension doesn't define the file type. If you want to verify a file type is provided (you probably don't need to worry about this if you're reading from the server filesystem, this is for input from users), you'd need to open the file and scan the header and then calculate it to ensure its a valid type.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,194
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Use the getimagesize function, it returns an array which includes the 'mime' type if it's an actual image file, as opposed to a misnamed text file, for example. It reads the actual file header so it isn't fooled.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

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