Php 不返回正确的 MIME 类型 [英] Php doesn't return the correct mime type

查看:107
本文介绍了Php 不返回正确的 MIME 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

finfo 函数正在返回疯狂的 mime 类型.看看下面的代码,这是怎么回事?

The finfo function is returning crazy mime types. Look the following code, what is going on?

<?php
    $files = array ("css.css", "index.html", "js.js", "png.png");

    $info = finfo_open (FILEINFO_MIME_TYPE);

    for ($i = 0; $i < count ($files); $i ++) {
        $type = finfo_file ($info, $files[$i]);

        $files[$i] = $type;
    }

    finfo_close ($info);

    echo $files[0]; // text/x-c -> WHAT ?!
    echo $files[1]; // text/html -> Ok !
    echo $files[2]; // text/x-c++ -> WHAT ?!
    echo $files[3]; // image/png -> Ok !
?>

谢谢

推荐答案

我不是很熟悉 fileinfo 的工作原理,但我认为这是正常的.文本文件(这就是 CSS 和 JS 的内容)没有提供关于它包含什么内容的明确指示.它们没有头字节,没有定义的结构.所以所有糟糕的 fileinfo 能做的就是猜测——结果很糟糕,正如你所看到的.

I'm not intimately familiar with the workings of fileinfo, but I think this is normal. Text files (and that's what CSS and JS are) provide no clear pointers as to what content it has. They have no header bytes, no defined structure. So all poor fileinfo can do is guess - with poor results, as you can see.

我认为要成功验证 .js 和 .css 文件的内容,您必须依赖扩展名,或者使用正确、适当的解析器实际解析它们.

I think to successfully verify the contents of .js and .css files, you have to either rely on the extension, or actually parse them with the correct, appropriate parser.

这篇关于Php 不返回正确的 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆