【问题标题】:Find Mime type of file or url using php for all file format使用 php 查找所有文件格式的 Mime 文件或 url 类型
【发布时间】: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


【解决方案1】:

在 PHP 中使用file_info 并以FILEINFO_MIME_TYPE 标志作为参数。

[示例取自 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 :

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

EDIT :

您需要在 PHP.ini 上启用扩展程序

;extension=php_fileinfo.dll

从该行中删除分号并重新启动您的网络服务器,您就可以开始了。
Installation Doc.

【讨论】:

  • 感谢您的快速回复 :) 我已经编辑了问题,我尝试了相同的功能,但它不起作用。我不知道为什么!没有错误来了!!! @香卡
  • @hitesh 检查你的 PHP 版本是什么
  • @hitesh,您是否取消了该行的注释,然后尝试重新启动?还要检查this
  • @hitesh,您需要将其作为一个新问题提出。你不能像这样继续更新你的问题......我认为这是你第四次更新它。
【解决方案2】:

找到答案了,

要查找文件的扩展名,最好的方法是对本地文件和 url 使用path_info

$info     = pathinfo($filename);

$basename = $info['basename'];

$ext      = $info['extension'];

为 mime 类型创建一个数组

$mimeTypes = array("mp4" =&gt; "video/mp4");// --> See Example hereHere

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

以上代码适用于 url 和本地文件。

注意:

那些在 CDN mp4 视频 mime 类型视频/mp4 问题上苦苦挣扎的人 - 我有 在我的 class.s3.php 文件中进行了更改-> 在 mime_type[] 数组中以及 使用 putObject() 函数进行交叉检查。

mime 类型的设置总是在编码端完成,而不是在 AWS S3 中 桶,我们需要使用AWS PHP Class file or sdk 来做 在 mime 类型中进行操作或在核心类中进行必要的更改 文件(例如 class.s3.php )

【讨论】:

【解决方案3】:
$type = image_type_to_mime_type(exif_imagetype($file));

【讨论】:

【解决方案4】:

在我尝试了以上所有这些解决方案之后,我就使用了:

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

$mimeTypeArr = shell_exec($command);

此命令返回以下行:

filename: mime_type

在我拥有这个之后,我会爆炸并得到我需要的东西。

$mimeTypeArr = shell_exec($command);

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

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

希望对你有帮助!

【讨论】:

  • 您可以使用file -b -i 来避免解析:使用-b 文件名不会附加到输出行。
猜你喜欢
  • 2021-06-11
  • 2011-06-25
  • 2015-09-20
  • 2011-02-02
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
相关资源
最近更新 更多