Find Mime type of file or url using php for all file format

28,393

Solution 1

Found the answer,

To find the extension of the file best way is to use path_info for both local files and url.

$info     = pathinfo($filename);

$basename = $info['basename'];

$ext      = $info['extension'];

create an array for the mime type

$mimeTypes = array("mp4" => "video/mp4");// --> See Example here and Here

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

Above code works for both url and local files.

Note :

Those struggling with CDN mp4 video mime type video/mp4 issue - I had made a change in my class.s3.php file -> in mime_type[] array and also cross checked with putObject() function.

Setting of mime type is always done in coding side and not in AWS S3 bucket, We need to use AWS PHP Class file or sdk to do the manipulation in mime type or make the necessary changes in core class file (eg. class.s3.php )

Solution 2

Make use of file_info in PHP with FILEINFO_MIME_TYPE flag as the parameter.

[Example taken as it is from 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 on your PHP.ini

;extension=php_fileinfo.dll

Remove the semicolon from that line and restart your webserver and you are good to go.
Installation Doc.

Solution 3

$type = image_type_to_mime_type(exif_imagetype($file));

Solution 4

After I tried all these solutions above that's what I use:

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

$mimeTypeArr = shell_exec($command);

This command returns the line below:

filename: mime_type

After I have this I explode and get what I need.

$mimeTypeArr = shell_exec($command);

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

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

Hope it helps!

Share:
28,393
Hitesh
Author by

Hitesh

On the Journey to become better and better everyday Thank God, I am not where I used to be, i am growing and I am on my way and I am heading toward the ultimate goal :) Aiming to become the best software developer, To develop the best quality and standard products. Love Learning new stuff to improve my skills. "Learning to Soar Like an Eagle…Even When You Feel Like a Chicken" I have just touched the surface of web development, There is so much here and still need to learn swimming :D And As They All Say "You don’t have to be a genius to code!" ;)

Updated on April 29, 2020

Comments

  • Hitesh
    Hitesh about 4 years

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

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

    I have 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;
    }
    

    Above code is not working for me, I am only seeing welcome for output.I am not sure if I am doing something 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));  // e.g. gives "image/jpeg"
    $mime  = explode(';', $mime_type);
    print $mime[0];
    

    Is there some work around which work for both(url and local).what is the best practice to set mime type for all contents (image, video, file etc.) other than mime_content_type function in php.also is it recommended to use the mime_content_type function in php, Is it best practice in php ?

  • Hitesh
    Hitesh over 10 years
    Thanks for quick reply :) I have edited the question, I had tried same function and it did not work.I am not sure why !!! no error is coming !!! @Shankar
  • Nathan H
    Nathan H over 10 years
    @hitesh check which version of PHP do you have
  • Shankar Narayana Damodaran
    Shankar Narayana Damodaran over 10 years
    @hitesh, Did you uncomment the line and then tried restarting ? Also check this
  • Shankar Narayana Damodaran
    Shankar Narayana Damodaran over 10 years
    @hitesh, You need to ask it as a new question. You just cant go on updating your question like this ... I think this is the 4th time you are updating it.
  • Hitesh
    Hitesh about 10 years
    Hi Thanks for the answer but i think php.net/manual/en/function.image-type-to-mime-type.php [image_type_to_mime_type] and [exif_imagetype][php.net/manual/en/function.exif-imagetype.p‌​hp] will only work for images not for other files such as video or document.Do you have any solution for that ?
  • Hitesh
    Hitesh about 10 years
    hmmm I thought so anyway thanks for the answer !!! it was helpful at least for images :)
  • alexglue
    alexglue about 9 years
    Ok, and what did u do if the link looks like "avatars2.githubusercontent.com/u/124070?v=3&s=400" ? There's no any extension in it!
  • lorenzo-s
    lorenzo-s almost 9 years
    You can use file -b -i to avoid parsing: with -b filenames are not prepended to output lines.
  • muglio
    muglio almost 9 years
    Didn't answer the op's question but you helped me :)