ratherBeKiting
ratherBeKiting

Reputation: 275

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.

Upvotes: 0

Views: 373

Answers (1)

MoeinPorkamel
MoeinPorkamel

Reputation: 711

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:

<?php

$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

Upvotes: 2

Related Questions