3

在过去的几个小时里,这让我很难过。

            // Using just straight up FINFO detects the mime type
            $oFileInfo = new finfo( FILEINFO_MIME_TYPE );
            $sMimeType = $oFileInfo -> file($_FILES['myfile']['tmp_name'] );
            // $sMimeType == 'application/pdf';

            // Using Zend File Transfer... detects the mime type
            $upload         = new Zend_File_Transfer_Adapter_Http();                
            $upload->addValidator('MimeType', true, array('application/pdf'));
            // $upload->getMimetype('myfile') == 'application/pdf'

            // But when I try to validate it throws an exception
            $upload->isValid('myfile');
            // Throws 
            // [fileMimeTypeFalse] => File 'myawesomefile.pdf' has a false mimetype of 'application/octet-stream'

该代码在 Apache、PHP 5.2 (Zend Server) 下运行良好,但现在它在 Nginx、PHP-FPM、PHP 5.3 下运行。我很想使用isValid()方法来验证我上传的文件,但 mime 类型不起作用。有什么建议么?

4

2 回答 2

2

上传的文件 mimetypes 永远不会太可靠......

我通常检查文件扩展名是一种悲伤的尝试,如果可能的话,尝试实际检查文件的内容。

如果是图像,你可以运行 getimagesize(),如果是 PDF,你可以创建一个验证器来调用 Zend_Pdf::load($filename) 并检查错误。可悲的是,即使是“图像”验证器也遇到了同样的问题。

于 2011-05-25T05:05:51.670 回答
1

当您上传或验证文件时,文件扩展名可能不正确,因为当您使用 .* 作为扩展名时会出现 application/octet-stream。

检查您的finfo类中的文件功能。

//$oFileInfo -> file($_FILES['myfile']['tmp_name'] );
于 2011-05-24T22:14:07.963 回答