php - 从php中的文件名获取mime类型

标签 php mime-types

我有以下函数从文件名生成 mime 类型:

    function get_mime_type($file) {
      if (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_file($finfo, $file);
        finfo_close($finfo);
      }
      else {
        $mimetype = mime_content_type($file);
      }
      if (empty($mimetype)) $mimetype = 'application/octet-stream';
      return $mimetype;
    }

我在代码的这一部分调用了这个函数:

        $out['uploads'][] = array(
          'filename' => $fldrow['field_value'],
          'mimetype' => get_mime_type($fldrow['field_value']),
          'id'       => $fldrow['ID'],
        );

$fldrow['field_value'] 包含 'card.pdf'

我期待着 'application/pdf'

我正在获取 'application/octet-stream'

我还使用 Mode=1 尝试了这种更精细的方法: PHP Mime type checking alternative way of doing it?

Mode=1 的结果相同,Mode=0 的结果为空白。

我在这里做错了什么?

编辑 我的解决方案基于 Dymen1 的响应并在查看该方向的其他帖子后如下:

function get_mime_type($filename) {
    $idx = explode( '.', $filename );
    $count_explode = count($idx);
    $idx = strtolower($idx[$count_explode-1]);

    $mimet = array( 
        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'docx' => 'application/msword',
        'xlsx' => 'application/vnd.ms-excel',
        'pptx' => 'application/vnd.ms-powerpoint',


        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    if (isset( $mimet[$idx] )) {
     return $mimet[$idx];
    } else {
     return 'application/octet-stream';
    }
 }

最佳答案

如果您检查文档,您会发现您没有做错任何事情。 但如果你做更多的研究:

https://stackoverflow.com/a/3664655/3784145

你可以看到你得到的mime类型是正确的,但是扩展不需要与这里解释的mime类型匹配:

http://nl3.php.net/manual/en/function.mime-content-type.php#85879

因此,我将使用文件后缀来确定文件的 mime 类型。 (如第一个示例所示)

关于php - 从php中的文件名获取mime类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35299457/

相关文章:

PHP - 同一页面上的 POST 和 GET 方法

python - .zip 文件在使用 gmail api 发送并使用 zlib 压缩时损坏

text - 文本的应用程序/普通 MIME 类型?

html - 如何创建 html 表单以发送多部分/混合 mime 类型的 http 消息

php切换大小写问题

javascript - 如何设置 mailto 表单结果的样式?

php - 将查询子句添加到 cakephp 查找操作中

php - 使用 jQuery 填写表单元素,但值未传递到表单操作

php - 上传的 zip 文件的 Mime 类型是 application/octet

node.js - 如何在node.js服务器的客户端下载存储在服务器中的pdf文件