【发布时间】:2014-03-21 07:47:27
【问题描述】:
您好,我正在寻找在 php 中找出任何本地文件或 url 的 mime 类型的最佳方法。 我已经尝试过 php 的 mime_content_type 函数,但由于它已被弃用,我正在为所有文件格式寻找更好的 php 解决方案。
mime_content_type — Detect MIME Content-type for a file ***(deprecated)***
下面的功能我已经试过了
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;
}
上面的代码对我不起作用,我只看到welcome 用于输出。我不确定我是否在这里做错了。
以下代码在我的本地以某种方式工作,但不适用于网址。
$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];
是否有一些适用于(url 和本地)的工作。什么是为所有内容(图像、视频、文件等)设置 mime 类型的最佳实践,而不是 php 中的 mime_content_type 函数。推荐在 php 中使用 mime_content_type 函数,这是 php 中的最佳实践吗?
【问题讨论】:
-
仅供参考
mime_content_type()不被弃用。在某些时候,它在在线手册中被意外标记为已弃用,但此后已得到纠正。
标签: php amazon-web-services mime-types content-type cdn