简体   繁体   English

如何确保上传的文件是MS Word文档?

[英]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) . 我正在考虑将上传的文档保存到webroot之外的文件夹中,并使用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. 我读过推荐使用finfo_open() ,但我的服务器是在php 5.3.0下,所以我不能使用它。 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). 我已经尝试过使用mime_content_type()但是它总是会给我发送一个“text / plain”和我发送的任何类型的文件(我不知道我是否做错了)。

Is there anything I could add to make this a more secure process? 有什么我可以添加,以使这个更安全的过程?

One issue you will inevitably come across is that browsers can tag a file's mime-type inappropriately. 您将不可避免地遇到的一个问题是浏览器可能会不恰当地标记文件的mime类型。 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. 例如,有一个常见的Firefox错误,可以将大多数文件标记为application / octet-stream,即使该文件确实是doc或pdf,或xls文件等。“最安全”的事情是扫描文件服务器端,其中还应包括病毒检查。 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. 我过去曾使用ClamAv( http://www.clamav.net/lang/en/ )来做到这一点。

See http://sourceforge.net/projects/php-clamav/ for more details. 有关详细信息,请参阅http://sourceforge.net/projects/php-clamav/

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

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