SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using fileinfo extension to determine a file's mime type

    I am trying to use the fileinfo extension to determine a file's mime type.

    When I run the following code:

    PHP Code:
    $file $_FILES['file']['tmp_name']; 
         
    $fhandle finfo_open(FILEINFO_MIME);

    $mime finfo_file($fhandle$file);
        
        
        
    if (
    $mime == 'image/jpeg')
    {
            
    echo 
    "fileinfo determined this to be an image/jpeg";
            

    but mime_type is not just the mime type (image/jpeg) but reads in full: "image/jpeg; charset=binary"

    So the comparison seems to be failing because of the extra "; charset=binary."

    I could take this string and just take the text before the semi-colon, but I heard that this extension could be used to fairly reliably check file types against known lists, so I think I may be overlooking some sort of formatting option for the finfo_file function, or something else altogether. Any thoughts on how to just capture "image/jpeg" into $mime_type instead of the full "image/jpeg; charset=binary" (I am not just looking for image files, I just used it as an example here.)

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I checked php.net and didn't find any suggestions. Any thoughts from people here ?

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the extension on php.net, just in case anyone is curious (and would like to assist!): http://php.net/manual/en/book.fileinfo.php

    It's a new extension as of php 5.3.


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
  •