Finding MIME type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ader
Forum Newbie
Posts: 2
Joined: Thu Jan 29, 2009 3:07 pm

Finding MIME type

Post by ader »

Hi!
I am trying to find the MIME type of a file, but the results I am getting are quite confusing. I am running Ubuntu 8.04.

The file with which I am testing is an OpenOffice writer file. My code is as follows:

Code: Select all

 
    $handle = finfo_open(FILEINFO_MIME);
    print(finfo_file($handle,$filename) . "\n");
 
This outputs "application/x-zip" which is clearly wrong or at least useless. If I, however, do a mimetype command in the console, the correct result is shown:

Code: Select all

application/vnd.oasis.opendocument.text
Running mimetype in debug mode, reveals the magic file used:

Code: Select all

Hashing magic from /usr/share/mime/magic
Trying now to let finfo use that magic file, however, returns an error:

Code: Select all

$handle = finfo_open(FILEINFO_MIME, '/usr/share/mime/magic');
Outputs:

Warning: finfo_open(): Failed to load magic database at '/usr/share/mime/magic'.

Has anyone run across this error before?

Yours sincerely
Rasmus
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Finding MIME type

Post by requinix »

It's a bug. Try creating a copy of that file named "magic.mime" in the same place. Don't change the code.

Also,
ader wrote:This outputs "application/x-zip" which is clearly wrong or at least useless.
Wrong, no, but it is useless. OOo's files are actually ZIP files with a different extension. Try it yourself: rename the file to *.zip and open it up.
The new DOCX format (I think) works the same way.
ader
Forum Newbie
Posts: 2
Joined: Thu Jan 29, 2009 3:07 pm

Re: Finding MIME type

Post by ader »

Thanks, I tried that. Now I am getting a heap of errors like the following one:

/usr/share/mime/magic.mime, 980: Warning offset `[10:text/x-tex]' invalid

After that, I am again getting the message that it "Failed to load magic database at '/usr/share/mime/magic'."

Any other ideas?

Rasmus
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Finding MIME type

Post by requinix »

Then finfo doesn't like that file. See if there's another magic file on the system somewhere, or maybe if your distro has a different version.
Post Reply