I have a PHP script that uploads files and returns the mimetype of the given file. This works very well with small filesizes but the moment I start using large-ish files (100mb+), the response degrades rapidly.
Is there a way to quickly retrieve the mimetype of a given file without loading it into memory or reading the entire file? As far as I know the mimetype is stored in the first few bytes of a file, so I shouldn't need to process the entire resource, correct? I've tried the following:
finfo_open(FILEINFO_MIME_TYPE) -> crashes on files over 60mb
file -bi -> Delay directly relates to size of file. Slow (10+ seconds)
xdg-mime query filetype your_file -> Slow. As above
I can't find any other PHP scripts/modules that could help. I'm sure that I'm not the only person who has experienced this, but my Google-fu seems lacking as I can't find any solutions at all.
I'm working on an Oracle Linux box, same as my production setup.
Thanks!
Edit: My files also do not have file extensions, they are stored with a unique ID much like
eed78421-7184-46f3-9c68-73315c47e3c8
If you can't rely to file extensions AND you want to avoid loading the whole file, what you are looking for are file signatures or magic numbers and where to get them. Wikipedia has an incomplete list that maps from magic numbers to file extensions (from which you can map to mime types).
https://en.wikipedia.org/wiki/List_of_file_signatures
Then you will need to implement something using a limited http://php.net/manual/en/function.fread.php to read some bytes and check for the magic numbers (you probably want to only look for a few specific ones).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With