get_file_type() similar to $_FILES['file']['type']?

Hey SP,

I’m trying to write a script that records a list of files in a directory filled with pdfs, exe’s, docs and plethora of other binary files and creates a download link to them. I would like to know if such a function exists that could give me the file type in the same format that $_FILES[‘file’][‘type’] returns on a file upload. For instance when I uploaded an .exe the superglobal $_FILES[‘file’][‘type’] will return to me “application/octet-stream” and various other strings. I would look to use this format and I can’t seem to find a function that can simply check and return this information of a file.

I am expecting something like

$file_type = get_file_type('/var/www/file_dir/afile.exe');

And I would write that into some kind of sql insert loop.

Any tips in the right direction would be helpful.

Well looks like I’ve managed to find a solution on this page. http://www.php.net/manual/en/ref.fileinfo.php

Well, it is hard to install and use this extension. There is better alternative - use lunux comand “file”. For insturctions - “man file” from linux shell.

<?
echo system("file -i -b file.pdf");
?>

application/pdf

If possible, I always like to use native linux commands. Thanks for checking out and reading anyhow.