ratherBeKiting ratherBeKiting - 1 year ago 119
MySQL Question

Detect MIME Type of file extracted from Database

I have files stored in a MySQL Database.

We need to find the MIME type of these files, to force files to open in a browser rather than download.

getimagesize and finfo wont work in this case as they require one parameter to be the path to a file rather than the file contents.

Is there a way to detect MIME type in PHP directly from the file contents.

Answer Source

You can get mime type using finfo_open and finfo_buffer. In this example i read content of a image from google and get its mimetype:


$Resource = finfo_open(FILEINFO_MIME);
$Info     = finfo_buffer($Resource, file_get_contents('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')); // Returns: image/png; charset=binary

list($Mime, $Other) = explode(";", $Info);

echo $Mime; // image/png
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download