简体   繁体   English

如何使我的检查MIME类型脚本正常工作? (PHP)

[英]How to get my checking MIME type script to work? (PHP)

For this script, it checks to see if the file is a microsoft words doc or ppt. 对于此脚本,它将检查文件是否为Microsoft Word doc或ppt。 I am not sure why this isn't running because it works for image MIME and text/plain. 我不确定为什么它不能运行,因为它适用于图像MIME和文本/纯文本。

I am using PHP 5.3.1 so it should have all the MIME types installed already right? 我正在使用PHP 5.3.1,因此应该已经安装了所有MIME类型,对吗?

I am uploading words and powerpoint 2007. 我正在上载Word和Powerpoint 2007。

//Does the file have the right MIME type?
if ($_FILES['userfile']['type'] !='application/msword') {
    echo 'Problem: file is not words doc.';
    exit;
}

IF I can't upload msword, is there a way to convert the words doc into a image type? 如果我无法上传msword,是否可以将doc文档转换为图像类型?

Images and Plain Text files are very different from proprietary files. 图像和纯文本文件与专有文件有很大不同。 Generally speaking, PHP/Web browsers can't support all proprietary file formats, so it is possible that the type is just returning as an octet-stream. 一般来说,PHP / Web浏览器不能支持所有专有文件格式,因此类型可能只是作为八位字节流返回。

The web browser is the part of the system that sends PHP the mime type. 网络浏览器是向PHP发送mime类型的系统的一部分。 Your browser might not properly send Microsoft Word mime types. 您的浏览器可能无法正确发送Microsoft Word MIME类型。 (But to say 'properly' is probably wrong, because I wouldn't expect any browser to try to identify word files) (但是说“正确”可能是错误的,因为我不希望任何浏览器尝试识别Word文件)

The reason image mime types are correctly sent is because images are fairly standard. 图像哑剧类型正确发送的原因是因为图像相当标准。 You have jpg, gif, etc, and that is about it. 您有jpg,gif等,就是这样。 But, this is only because they are standard, open formats, and have been around for a long time. 但是,这仅仅是因为它们是标准的开放格式,并且已经存在了很长时间。

Other formats such as .doc, .psd, etc, are unlikely to be sent, as they aren't really standard. 其他格式(例如.doc,.psd等)不太可能发送,因为它们并不是真正的标准。

As @chacha already says, it is well possible the browser doesn't recognize a Word file's MIME type, and thus doesn't send it along with the request. 正如@chacha已经说过的那样,浏览器很有可能无法识别Word文件的MIME类型,因此不会将其与请求一起发送。 It may depend on what applications are installed on the client machine. 这可能取决于客户端计算机上安装了哪些应用程序。 Basic file types like images are likely to be recognized by any browser. 基本文件类型(例如图像)很可能会被任何浏览器识别。

For security purposes, checking the MIME type is useless anyway, as it can be forged by the client. 为了安全起见,检查MIME类型无论如何都是无用的,因为它可以由客户端伪造。

If I were you, I would not do any checking at all (or, if you want to warn the user when they're about to upload the wrong file, only check for the file extension) and be very careful when processing the file. 如果您是我,则根本不会进行任何检查(或者,如果您要在用户要上传错误的文件时警告用户,请仅检查文件扩展名),并且在处理文件时要非常小心。 If I remember correctly, the new Office 2007 files are ZIP archives containing XML and other files - you will notice a corrupted file the second you try to unzip it. 如果我没记错的话,新的Office 2007文件是包含XML和其他文件的ZIP存档-您尝试第二次解压缩时会注意到文件已损坏。

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

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