PHP get mime type poor performance

Viewed 326

Does anyone know why getting mime type in PHP is relatively slow and memory-hungry? It's even much slower than getimagesize(), which also includes the mime type (for images). I have been working on an app that gets various info about files in a dir, and I noticed that getting mime slowed down the script and increased memory exponentially.

I am aware that getting mime requires reading the file (in some way or other), but at this point I am tempted to use an extension-to-mimetype array lookup instead. Tested on two servers with average output:

$file = './larotate.JPG';
for ($i=0; $i < 1000; $i++) $t = mime_content_type($file); // "7.107s, 10.4M"
for ($i=0; $i < 1000; $i++) $t = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file); // "7.636s, 10.4M"
for ($i=0; $i < 1000; $i++) $t = getimagesize($file); // "0.063s, 0.4M"

Time and memory consumed in comment.

If this is just how it is, I can live with that, although I'm slightly surprised. My script gets an array of data about all files in a dir, including size, dimensions, readable, permissions, time etc. Getting mime type slows down the script exponentially, so I am considering using mime lookup array (from extension) instead, although this may not always be accurate.

0 Answers
Related