Find mim file type or url using php for all file formats

Hi, I am looking for the best way to find out the mime type in php for any local file or url. I tried the mime_content_type php function, but since it is deprecated, I am looking for the best solution in php for all file formats.

mime_content_type — Detect MIME Content-type for a file ***(deprecated)*** 

I already tried below function

 echo 'welcome'; if (function_exists('finfo_open')) { echo 'testing'; $finfo = finfo_open(FILEINFO_MIME); $mimetype = finfo_file($finfo, "http://4images.in/wp-content/uploads/2013/12/Siberian-Tiger-Running-Through-Snow-Tom-Brakefield-Getty-Images-200353826-001.jpg"); finfo_close($finfo); echo $mimetype; } 

The code does not work for me, I see only welcome for output. I'm not sure I'm doing anything wrong here.




Below code works somehow in my local, but it does not work for URLs.

 $file = './apache_pb2.png'; $file_info = new finfo(FILEINFO_MIME); // object oriented approach! $mime_type = $file_info->buffer(file_get_contents($file)); // eg gives "image/jpeg" $mime = explode(';', $mime_type); print $mime[0]; 

Is there some kind of work that works for both (url and local). What is the best practice for setting the mime type for all content (image, video, file, etc.), except for the mime_content_type function in php.also, it is recommended to use the mime_content_type function in php, is this better in php?

+6
content-type mime-types php amazon-web-services cdn
Feb 20 '14 at 11:36
source share
4 answers

Found the answer

To find the file extension, the best way is to use path_info for local files and URLs.

 $info = pathinfo($filename); $basename = $info['basename']; $ext = $info['extension']; 

create an array for mime type

$mimeTypes = array("mp4" => "video/mp4");//See an example here and here

 //get the mime type for file $type = isset($this->mime_types[$ext]) ? $this->mime_types[$ext] : "application/octet-stream"; 

The code works for both URLs and local files.

Note:

Those who are struggling with CDN mp4 video mime type video / mp4 issue - I had made changes to my class.s3.php → in mime_type [] file, as well as cross-checking with the putObject () function.

Setting the mime type is always performed in the encoding, and not in the AWS S3 bucket, we need to use the AWS PHP Class file or sdk to perform manipulations in the mime type or make the necessary changes to the main class file (for example, class.s3.php)

+5
Feb 25 '14 at 12:21
source share

Use file_info in PHP with the FILEINFO_MIME_TYPE flag as a parameter.

[Example taken from the PHP manual]

 <?php $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension foreach (glob("*") as $filename) { echo finfo_file($finfo, $filename) . "\n"; } finfo_close($finfo); ?> 

OUTPUT :

 text/html image/gif application/vnd.ms-excel 



EDIT :

You need to enable the extension in PHP.ini

 ;extension=php_fileinfo.dll 

Remove the semicolon from this line and reload the web server, and you're good to go.
Installation Doc.

+10
Feb 20 '14 at 11:37
source share
 $type = image_type_to_mime_type(exif_imagetype($file)); 
+3
Apr 24 '14 at
source share

After I tried all these solutions above what I use:

$command = "file -i {$filename}";

$mimeTypeArr = shell_exec($command);

This command returns the following line:

filename: mime_type

After that, I explode and get what I need.

$mimeTypeArr = shell_exec($command);

$mimeTypeArr = explode(':', $mimeTypeArr);

$mimeType = trim($mimeTypeArr[1]);

Hope this helps!

+2
Apr 29 '14 at 7:35
source share



All Articles