www.webdeveloper.com
Results 1 to 6 of 6

Thread: Getting file type

  1. #1
    Join Date
    May 2005
    Posts
    57

    Getting file type

    I am in need to get the file type of the given file. I mean whether the given file is jpg or bmp or doc or xls. Please donot suggest me to check the file extension. (There might be a chance to change the extension of a doc file to jpg).

    Thanks in Advance

  2. #2
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Read file headers, as in the binary data that is stored within the first X bytes of the file.

  3. #3
    Join Date
    May 2005
    Posts
    57
    Quote Originally Posted by MrCoder
    within the first X bytes of the file.
    You mean first 10 bytes of the file?.

  4. #4
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Why 10?

    For example I believe the first 4 bytes of some formatted wave files start with "RIFF"

  5. #5
    Join Date
    May 2005
    Posts
    57
    I thought X -> 10 (Roman indication).

    Is there any unique format in representing the file formats, so that i can able to read the bytes and get the type of the file.

  6. #6
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Look at this page, some very useful ones listed here.

    There is also a very good list here

    You could also, if hosted on Linux, use the "file" command.

    Code:
    file /path/to/my/file/myfile.php
    Would output..

    Code:
    /path/to/my/file/myfile.php: PHP script text
    OR...
    mime_content_type
    PHP Code:
    echo mime_content_type("/path/to/my/file/myfile.php"); 
    Would output
    Code:
    text/plain
    OR...
    fileinfo
    Last edited by MrCoder; 12-19-2007 at 08:48 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles