Why Zend Framework Validate fail for MimeType when calling isValid ()

This has stopped me for the last few hours.

            // 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'

The code worked fine under Apache, PHP 5.2 (Zend Server), but now it is under Nginx, PHP-FPM, PHP 5.3. I would like to use the isValid () method to check my downloaded file, but the mime type does not work. Any suggestions?

+3
source share
2 answers

Mimetypes downloads are never too reliable ...

I usually check the file extension as a rude attempt, and if at all possible, try checking the contents of the file.

, getimagesize(), PDF, Zend_Pdf:: load ($ filename) . , "Image" .

+2

, , , /- , . * .

finfo.

//$oFileInfo -> file($_FILES['myfile']['tmp_name'] );
+1

All Articles