Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying Uploaded File

I am working on a document that requires a user to upload a Microsoft Word Documents.

Apart from checking the file extension to be .doc or .docx, is there any other way i can verify that the uploaded file is actually a Microsoft Word Document and not any other file renamed to a .doc or .docx extension.

Thanks in advance.

like image 989
IndexController Avatar asked Aug 04 '26 03:08

IndexController


2 Answers

If your are not using PHP 5.3, the mime_content_type function might interest you.

If you are using PHP 5.3 and/or can install PECL extensions, the new Fileinfo library should do the job ; see finfo_file for more informations.
In the given example, one of the identified mime types is "application/vnd.ms-excel" ; so, with a bit of luck, it should be able to deal with MS Word files too ;-)

like image 77
Pascal MARTIN Avatar answered Aug 05 '26 17:08

Pascal MARTIN


.docx is a set of XML files that have been compressed using the standard zip compression scheme. So you could try passing it to an unzip algorithm and seeing if it decompresses, and then attempt to look at the proper xml file within and check for fields that one would expect to find in a document.

like image 34
Amber Avatar answered Aug 05 '26 16:08

Amber