Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure that an uploaded file is an MS Word document?

I'm thinking of saving uploaded docs to a folder outside the webroot and feeding the downloads with a script using readfile(file).

However, I'm wondering if the the following would be enough to remove any kind of threats there could be:

$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "doc") && ($_FILES["uploaded_file"]["type"] == "application/msword"))
{
execute rest of the code
}

I've read people recommended using finfo_open(), but my server is under php 5.3.0 so I can't use it. I've tried using mime_content_type() but it will always throw me a "text/plain" with any kind of file I send through (I don't know if I'm doing something wrong with that).

Is there anything I could add to make this a more secure process?

like image 427
Filgera Avatar asked Jun 15 '11 19:06

Filgera


People also ask

How do you upload a File to Microsoft Word?

In the Documents area, click New Document and then click Upload Existing File. Tips: You can also drag or drop files or save directly to your team site or OneDrive for Business from Office.

Is Google Docs Microsoft Word?

Google Docs is a free, web-based alternative to Microsoft Word. All you need is a Google account, an internet connection, and these tips and tricks.

Why does my Word document look different when I email it?

Chances are that your document is set to update styles from the template upon opening. You need to change this setting. Go to the Developer tab and click on Add-Ins. If "Automatically update document styles" is checked, uncheck it.


1 Answers

One issue you will inevitably come across is that browsers can tag a file's mime-type inappropriately. For example there is a common Firefox bug that can tag most files as application/octet-stream even though the file genuinely is a doc or pdf, or xls file, etc. The 'safest' thing to do is scan the file server side, which should also include a virus check. However if you have limited server access you may not be able to do this.

I've used ClamAv (http://www.clamav.net/lang/en/) in the past to do this.

See http://sourceforge.net/projects/php-clamav/ for more details.

like image 169
Colin Avatar answered Oct 26 '22 11:10

Colin