Results 1 to 17 of 17

Thread: [RESOLVED] File Content Type

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Resolved [RESOLVED] File Content Type

    is there an easy way to get the content type of a file?

    I am working on a file sharing script and i need to get the file content type for download.

    the only way i can think of is like having a database with all the different content types and getting it by the file extension...
    My usual boring signature: Something

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Content Type

    You could use the MIMEType extension to sniff for a content type. It is more reliable however to store the content type when the file is uploaded if possible.

  3. #3
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: File Content Type

    If you are on a *nix system, you can tell the script to execute:
    Code:
    file -bi file
    And that will return the content type
    :~$ file -bi hosts.allow
    text/plain; charset=us-ascii
    In fact this is what mediawiki does. Or can do I should say.

    If you are on a windows box, there probably is a port of that program. And if you really wanted to get the content type, that would be the best way imo.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  4. #4

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: File Content Type

    Quote Originally Posted by penagate
    You could use the MIMEType extension to sniff for a content type. It is more reliable however to store the content type when the file is uploaded if possible.
    there are so many though. it would take forever to get them all.
    My usual boring signature: Something

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: File Content Type

    Quote Originally Posted by dclamp
    is there an easy way to get the content type of a file?

    I am working on a file sharing script and i need to get the file content type for download.

    the only way i can think of is like having a database with all the different content types and getting it by the file extension...
    It is very difficult determine the exact file type of an external file; even when that file is just a text file. If the user uploading the file wants others to know the true file type then they will use the appropriate extension from which the browser can determine and send the correct MIME type for.

    If the user wants to disguise the file type they can change the extension. To determine the true type you may need to open the file and check that it conforms to the appropriate format (e.g: XML,CSV). This is more difficult with binary files that have undocumented file formats.

    Checking for executable files may involve looking for specific binary instruction combinations; but is by no means reliable and is even more difficult if the user has encoded or encrypted the file. In short; if you are doing this from a security perspective; the controls you can put in place a limited.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Content Type

    Quote Originally Posted by dclamp
    there are so many though. it would take forever to get them all.
    Many what? A file can only have one content type. I'm not sure what you mean.


    When a file is uploaded, the MIME type is sent by the client and available in the $_FILES array.

    Maybe if you clarify what you are doing we can provide better assistance.

  7. #7

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: File Content Type

    oh. the $_FILES tells you what MIME type it is? i thought i would have to write code to figure out what MIME type it is
    My usual boring signature: Something

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Content Type

    $_FILES[$index]['type']

  9. #9
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: File Content Type

    It is only available if its sent by the browser.
    I don't know under what circumstances it doesn't, but I wouldn't touch it with a 6ft pole. I would use file -bi, that way you know for sure what the mime type is.

    See http://www.php.net/manual/en/feature...load.php#53133
    Also: http://www.php.net/manual/en/feature...load.php#52989
    And: http://www.php.net/manual/en/feature...load.php#51256
    Last edited by k1ll3rdr4g0n; Mar 30th, 2008 at 10:45 PM.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Content Type

    You can't know for sure. All file with the -i flag does is use a 'magic' behaviour which is the same as the MIMEType and PECL/fileinfo extension. This is fallible; all type sniffing is. visualAd is correct — if this is for security purposes, there is not a lot you can realistically do.

    Relying on the MIME type sent by the client is fine if you just need it to send back when streaming the file for download to another client. If the type is missing, you can simply use application/octet-stream to force a download. This is more than adequate for a file sharing service.

  11. #11

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: File Content Type

    i used application/octet-stream for a .doc file, and Word wasnt able to open it after i downloaded it. it said it was corrupt
    My usual boring signature: Something

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Content Type

    That suggests you are not streaming it correctly...

  13. #13

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: File Content Type

    should i show you my code then?
    My usual boring signature: Something

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Content Type

    At your leisure.

    In fact, don't post it in code tags. Attach the .php file (if possible).

  15. #15

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: File Content Type

    thanks all. the $_FILES['file']['type'] solved it!
    My usual boring signature: Something

  16. #16
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] File Content Type

    Dylan, the first four bytes of the file you upload can also be used to verify the file type. A JPEG file for example should always start with "ff d8 ff e0", if it doesn't then you can be absolutely sure that the file is not a JPEG. That doesn't prevent other data from being hidden within it but is is another step you can use:
    PHP Code:
    // example
    $jpeg chr(0xff) . chr(0xd8) . chr(0xff) . chr(0xe0);

    $fhwnd fopen('file.jpg''rb');

    $signature fread($fhwnd4);

    if (
    $signature != $jpeg) {
        
    // wrong file type

    You can see some other common signatures here: http://www.garykessler.net/library/file_sigs.html. Watch out though, some file types may have several signatures.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  17. #17

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: [RESOLVED] File Content Type

    thanks for that vis. i will look into it!
    My usual boring signature: Something

Posting Permissions

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



Click Here to Expand Forum to Full Width