简体   繁体   English

为同一文件检测到不同的文件哑剧类型

[英]Different file mime type detected for same file

I'm uploading some files. 我正在上传一些文件。 Below is part of the script for the upload (I'm using codes from https://github.com/blueimp/jQuery-File-Upload/blob/master/php/index.php ) 以下是上传脚本的一部分(我正在使用来自https://github.com/blueimp/jQuery-File-Upload/blob/master/php/index.php的代码)

$upload = isset($_FILES[$this->options['param_name']]) ?
                $_FILES[$this->options['param_name']] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
    foreach ($upload['tmp_name'] as $index => $value) {
    $info[] = $this->handle_file_upload(
    $upload['tmp_name'][$index],
            isset($_SERVER['HTTP_X_FILE_NAME']) ?
                  $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
            isset($_SERVER['HTTP_X_FILE_SIZE']) ?
                  $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
            isset($_SERVER['HTTP_X_FILE_TYPE']) ?
                  $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
                   $upload['error'][$index]
     );
}

When checked, the mime type through $_SERVER['HTTP_X_FILE_TYPE'] or $upload['type'][$index] returns application/vnd.openxmlformats-officedocument.presentationml.presentation , application/vnd.openxmlformats-officedocument.wordprocessingml.document for pptx , docx files respectively. 选中后,通过$_SERVER['HTTP_X_FILE_TYPE']$upload['type'][$index]的MIME类型返回application/vnd.openxmlformats-officedocument.presentationml.presentationapplication/vnd.openxmlformats-officedocument.wordprocessingml.document分别用于pptxdocx文件。

Now after completing the upload, I'm trying to display the files and I'm using finfo_file() to get the mime type. 现在,完成上传后,我尝试显示文件,并使用finfo_file()获取MIME类型。

$mime_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file_path);
echo $mime_type;

For images files (like png, jpeg) & pdf, the type returns as expected but for the pptx and docx files, the mime type returned is application/zip . 对于图像文件(如png,jpeg)和pdf,其类型按预期返回,但对于pptxdocx文件,返回的mime类型为application/zip

Why is it not returning the same as while in uploading? 为什么上传时返回的不一样? Is there something wrong with my code or should I expect this? 我的代码有什么问题吗?还是应该期待这一点? How do I decide which file type it is then? 我该如何确定它是哪种文件类型?

The same file can have different and mulitple mime-types, that is totally normal. 相同的文件可以具有不同的,多个mime类型,这是完全正常的。

Additionally the mime-type is only meta-information next to the file itself. 另外,mime类型只是文件本身旁边的元信息。 Theoretically you can give any file any mime-type. 从理论上讲,您可以为任何文件指定任何mime类型。 That would not be very useful, but it works. 那不是很有用,但是可以。 It's just a concept. 这只是一个概念。

The finfo library will try to obtain the mime-type of a file "magically" by looking into the file trying to identify the format. finfo库将通过查看试图识别格式的文件来尝试“神奇地”获取文件的mime类型。 Then it will return the mime-type according to it's database. 然后它将根据其数据库返回mime-type。

Why is it not returning the same as while in uploading? 为什么上传时返回的不一样?

The mime-type within the request is given by the HTTP client. 请求中的mime类型由HTTP客户端提供。 It might guess as well, but often it takes the value from information the underlying operating system is giving for that file. 它也可能会猜测,但是通常它会从底层操作系统为该文件提供的信息中获取价值。

As you can see with your issue that the more common the file-type is, the better it will match (the images). 正如您在问题中看到的那样,文件类型越常见,匹配的文件(图像)就越好。

However as pptx and docx files are actually zip-files, the finfo library will identify those as application/zip because the headers of those files (magic numbers) show that it is technically a zip file. 但是,由于pptx和docx文件实际上是zip文件,因此finfo库会将这些文件标识为application/zip因为这些文件的标题(魔术数字)表明它在技术上是zip文件。

Is there something wrong with my code or should I expect this? 我的代码有什么问题吗?还是应该期待这一点?

You should not expect that the mime-type of finfo matches the request header mime-type. 您不应期望finfo的mime-type与请求标头mime-type相匹配。 Those are two different things. 那是两件事。

How do I decide which file type it is then? 我该如何确定它是哪种文件类型?

That depends. 那要看。 You can decide to trust the http header, you can decide to trust finfo , you can decide to compare the file extensions as well and a combination of all three. 您可以决定信任http标头,可以决定信任finfo ,还可以决定比较文件扩展名以及所有这三个文件的组合。

Additionally you can decide to even add more. 另外,您可以决定添加更多。 This entirely depends on what you do with the uploaded file. 这完全取决于您对上传文件的处理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM