0

I used to get different MIMI types when using finfo_open(FILEINFO_MIME_TYPE).

For instance, based on memory, I believe docx were application/vnd.openxmlformats-officedocument.wordprocessingml.document (see this StackOverflow post What is a correct mime type for docx, pptx etc?). I also recall them showing as application/zip, but that might have been when using mime_content_type(). Also, thought js files came up as application/x-javascript.

Below is what I am now getting along with the script to display them. I recently updated to PHP 5.14, and wonder if there is a new standard or whether I messed something up. I am fine with this, but will have to change my scripts which compared extensions to MIMI types.

EDIT - Just found the following on the PHP manual.

As of PHP >= 5.3.11 and >= 5.4.1 the magic datababase format has changed. Due to this, the internal mime database was upgraded. This affects especially the code where external magic is read in. Reading in magic files of the older formats will not work. Also, some textual representations of the mime types has changed, for instance for PHP would be "PHP script, ASCII text" instead of "PHP script text" returned.

Thank you

MIMI List based on below script

- directory
csv - text/plain
doc - application/msword
docx - application/msword
htm - text/html
ini - text/plain
jpg - image/jpeg
js - text/plain
mp3 - application/octet-stream
pdf - application/pdf
php - text/html
pptx - application/vnd.ms-powerpoint
pst - application/octet-stream
so - application/x-sharedlib
txt - text/plain
wmv - video/x-ms-asf
xls - application/msword
xlsx - application/vnd.ms-excel
zip - application/zip

PHP Code

<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
    $files[pathinfo($filename, PATHINFO_EXTENSION)]=array('filename'=>$filename,'finfo'=>finfo_file($finfo, $filename));
}
ksort($files);
foreach ($files as $fileext=>$file) {
    echo ($fileext.' - '.$file['finfo'].'<br />');
}
finfo_close($finfo);
?>
Community
  • 1
  • 1
user1032531
  • 24,028
  • 57
  • 174
  • 325

1 Answers1

0

The mime types printed will differ based on the system's configuration. If the system is configured to open .docx files in MSWord, then it'll have application/msword as the mime type. Likewise, if the same extension .docx is configured to open in LibreOffice, it'll have application/vnd.openxmlformats-officedocument.wordprocessingml.document as the mime type.

Because of this dependency on the server's configuration, mime types are not very reliable.

Sean Johnson
  • 5,443
  • 2
  • 14
  • 22
  • What is the most common configuration? Is there a better way to ensure that an uploaded file's extension matches the file type? – user1032531 Jun 29 '12 at 15:40