简体   繁体   English

上载ZIP文件的MIME类型错误

[英]Wrong MIME-Type for uploaded ZIP files

How do I handle MIME-Type validation of an uploaded ZIP file in both Linux and Windows environments? 如何在Linux和Windows环境中处理上载的ZIP文件的MIME类型验证?

I created a testing purposes ZIP file and downloaded another one (first Google result) and use them as arguments of the following code: 我创建了一个用于测试的ZIP文件,并下载了另一个文件(第一个Google结果),并将其用作以下代码的参数:

$finfo = new finfo( FILEINFO_MIME );

echo $finfo -> file( 'benchmark.zip' ), '<br />', $finfo -> file( 'css3menu.zip' ), '<br />';

echo mime_content_type( 'benchmark.zip' ), '<br />', mime_content_type( 'css3menu.zip' );

It reported me: 它报告我:

application/zip; charset=binary
application/zip; charset=binary
application/zip
application/zip

In both environments, Linux, using Apache, and Windows, with PHP built-in webserver. 在两种环境中,使用Apache的Linux和使用PHP内置Web服务器的Windows。

After reading this answer I also considered application/octet-stream as valid so I added them both to my Upload Handler. 阅读此答案后,我还认为application / octet-stream有效,因此将它们都添加到了我的上载处理程序中。

But the upload failed, reporting me invalid file type :( 但是上传失败,报告我无效的文件类型:(

I checked everything and it was correct, so I decided to isolate the problem with the most basic upload implementation possible (together with that debugging code): 我检查了所有内容,发现它是正确的,因此我决定使用最基本的上载实现(连同该调试代码)来隔离问题:

if( isset( $_POST['button'] ) ) {

    print '<pre>'; print_r( $_FILES );

    $finfo = new finfo( FILEINFO_MIME );

    echo $finfo -> file( 'benchmark.zip' ), '<br />', $finfo -> file( 'css3menu.zip' ), '<br />';

    echo mime_content_type( 'benchmark.zip' ), '<br />', mime_content_type( 'css3menu.zip' ), '<br />';
}

?>

<form enctype="multipart/form-data" action="http://localhost/test.php" method="post">

<input type="file" name="file[]" id="file" />

<input type="submit" name="button" value="Send" />

</form>

The MIME-Type reported by FileInfo and mime_content_type() were the same as before, but the type index in $_FILES returned application/vnd.zend.serverconfig in Linux environment and the less desirable, although valid, application/octet-stream in Windows. FileInfo和mime_content_type()报告的MIME类型与以前相同,但是$ _FILES中的类型索引在Linux环境中返回了application / vnd.zend.serverconfig,而在Windows中则不太理想,尽管有效,但application / octet流

Any ideas of why this is happening? 为什么会这样?

One thing that worth to mention is I'm new in Linux environment so I didn't know how to setup a webserver so, at first, I installed Zend Server but later I uninstalled it completelly and reinstall everything manually using apt-get. 值得一提的是,我是Linux环境的新手,所以我不知道如何设置Web服务器,因此,起初我安装了Zend Server,但后来我完全卸载了它,并使用apt-get手动重新安装了所有内容。

After understanding that the type index is provided by the browser and not by PHP I tested in Google Chrome and got the correct application/zip 了解类型索引是由浏览器而不是PHP提供的后,我在Google Chrome中进行了测试,并获得了正确的应用程序/ zip

So I Google'd how to fix this issue in Firefox, the browser I was testing before, and all I needed to do was delete the mimeTypes.rdf file in the profile folder located in ~/.mozilla/firefox/ 因此,我用Google解决了在以前测试过的Firefox浏览器中的问题,而我要做的就是删除〜/ .mozilla / firefox /中的配置文件文件夹中的mimeTypes.rdf文件。

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

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