简体   繁体   中英

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. I am not sure why this isn't running because it works for image MIME and text/plain.

I am using PHP 5.3.1 so it should have all the MIME types installed already right?

I am uploading words and 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?

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.

The web browser is the part of the system that sends PHP the mime type. Your browser might not properly send Microsoft Word mime types. (But to say 'properly' is probably wrong, because I wouldn't expect any browser to try to identify word files)

The reason image mime types are correctly sent is because images are fairly standard. You have jpg, gif, etc, and that is about it. 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.

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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