使用 php 为所有文件格式查找 Mime 类型的文件或 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21906596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Mime type of file or url using php for all file format
提问by Hitesh
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_typefunction of php but since it is deprecated I am looking for better solution in php for all file format.
嗨,我正在寻找在 php 中为任何本地文件或 url 找出 mime 类型的最佳方法。我已经尝试过php 的mime_content_type函数,但由于它已被弃用,我正在为所有文件格式在 php 中寻找更好的解决方案。
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 welcomefor output.I am not sure if I am doing something wrong here.
上面的代码对我不起作用,我只看到welcome输出。我不确定我在这里做错了什么。
Below code works somehow in my local but it does not work for urls.
下面的代码在我的本地以某种方式工作,但它不适用于 url。
$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 ?
是否有一些工作可以同时适用于(url 和本地)。为 php 中的 mime_content_type 函数以外的所有内容(图像、视频、文件等)设置 mime 类型的最佳做法是什么。还建议使用php 中的 mime_content_type 函数,它是 php 中的最佳实践吗?
回答by Shankar Damodaran
Make use of file_infoin PHP with FILEINFO_MIME_TYPEflag as the parameter.
利用file_infoPHP中的FILEINFO_MIME_TYPE标志作为参数。
[Example taken as it is from PHP Manual]
[示例取自 PHP 手册]
<?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 :
OUTPUT :
text/html
image/gif
application/vnd.ms-excel
EDIT :
EDIT :
You need to enable the extension on your PHP.ini
您需要在您的设备上启用扩展 PHP.ini
;extension=php_fileinfo.dll
Remove the semicolon from that line and restart your webserver and you are good to go. Installation Doc.
从该行中删除分号并重新启动您的网络服务器,您就可以开始了。 Installation Doc.
回答by Hitesh
Found the answer,
找到了答案,
To find the extension of the file best way is to use path_infofor both local files and url.
要找到文件的扩展名,最好的方法是对本地文件和 url使用path_info。
$info = pathinfo($filename);
$basename = $info['basename'];
$ext = $info['extension'];
create an array for the mime type
为 mime 类型创建一个数组
$mimeTypes = array("mp4" => "video/mp4");//--> See Example hereand Here
$mimeTypes = array("mp4" => "video/mp4");//-->请参阅此处和此处的示例
//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.
上面的代码适用于 url 和本地文件。
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 sdkto do the manipulation in mime type or make the necessary changes in core class file (eg. class.s3.php )
笔记 :
那些在 CDN mp4 视频 mime 类型视频/mp4 问题上苦苦挣扎的人 - 我在我的 class.s3.php 文件中进行了更改 -> 在 mime_type[] 数组中,并且还与 putObject() 函数进行了交叉检查。
mime 类型的设置总是在编码端完成,而不是在 AWS S3 存储桶中,我们需要使用
AWS PHP Class file or sdkmime 类型进行操作或在核心类文件(例如 class.s3.php )中进行必要的更改
回答by lbsweek
$type = image_type_to_mime_type(exif_imagetype($file));
回答by Galya Ts.
After I tried all these solutions above that's what I use:
在我尝试了上述所有这些解决方案之后,我使用了以下方法:
$command = "file -i {$filename}";
$command = "file -i {$filename}";
$mimeTypeArr = shell_exec($command);
$mimeTypeArr = shell_exec($command);
This command returns the line below:
此命令返回以下行:
filename: mime_type
filename: mime_type
After I have this I explode and get what I need.
有了这个之后,我就会爆炸并得到我需要的东西。
$mimeTypeArr = shell_exec($command);
$mimeTypeArr = shell_exec($command);
$mimeTypeArr = explode(':', $mimeTypeArr);
$mimeTypeArr = explode(':', $mimeTypeArr);
$mimeType = trim($mimeTypeArr[1]);
$mimeType = trim($mimeTypeArr[1]);
Hope it helps!
希望能帮助到你!

