最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Php doesn't return the correct mime type - Stack Overflow

programmeradmin4浏览0评论

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 !
?>

Thanks

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 !
?>

Thanks

Share Improve this question edited Apr 25, 2016 at 8:48 jotik 17.9k16 gold badges65 silver badges129 bronze badges asked Mar 7, 2011 at 22:56 CaioCaio 3,2156 gold badges40 silver badges53 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 9

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.

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.

At present, there seems to be a bug with finfo

https://bugs.php.net/bug.php?id=53035

it's got to do with the content of the actual mime database, as opposed to any erroneous logic.

What I'm doing (which may not be as useful for more rigourous situations) is hard code the correct mime types that I know I'll need so that the hard coding will simply need to be commented out for the next version of PHP. À la:

$info = finfo_open(FILEINFO_MIME_TYPE);     
$mime_type = finfo_file($info, $file_name);
$extension = pathinfo($file_name,PATHINFO_EXTENSION);

//there is a bug with finfo_file();
//https://bugs.php.net/bug.php?id=53035
//
// hard coding the correct mime types for presently needed file extensions
switch($extension){

    case 'css':
        $mime_type = 'text/css';
    break;
    case 'js':
        $mime_type = 'application/javascript';
    default:
    break;
}

check this

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

    for ($i = 0; $i < count ($files); $i ++) {
        $files[$i] = preg_replace("%.*\.(\w)%i", "$1", $files[$i]);
    }

    echo $files[0]; //css
    echo $files[1]; //html
    echo $files[2]; //js
    echo $files[3]; //png
?>

I recently faced a situation where an uploaded text file was wrongly recognized as "text/x-c++" because it began with the keyword class . It was correctly recognized as "text/plain" with another first word. It was indeed probably related to the browser recognition algorithm, as this also occurred on this website : https://htmlstrip.com/mime-file-type-checker

Have you checked your server's mime type definitions? I assume it uses the servers definitions just like browsers use the client computers definition for uploaded files.

发布评论

评论列表(0)

  1. 暂无评论