How do I handle MIME-Type validation of an uploaded ZIP file in both Linux and Windows environments?
I created a testing purposes ZIP file and downloaded another one (first Google result) and use them as arguments of the following code:
$finfo = new finfo( FILEINFO_MIME );
echo $finfo -> file( 'benchmark.zip' ), '<br />', $finfo -> file( 'css3menu.zip' ), '<br />';
echo mime_content_type( 'benchmark.zip' ), '<br />', mime_content_type( 'css3menu.zip' );
It reported me:
application/zip; charset=binary
application/zip; charset=binary
application/zip
application/zip
In both environments, Linux, using Apache, and Windows, with PHP built-in webserver.
After reading this answer I also considered application/octet-stream as valid so I added them both to my Upload Handler.
But the upload failed, reporting me invalid file type :(
I checked everything and it was correct, so I decided to isolate the problem with the most basic upload implementation possible (together with that debugging code):
if( isset( $_POST['button'] ) ) {
print '<pre>'; print_r( $_FILES );
$finfo = new finfo( FILEINFO_MIME );
echo $finfo -> file( 'benchmark.zip' ), '<br />', $finfo -> file( 'css3menu.zip' ), '<br />';
echo mime_content_type( 'benchmark.zip' ), '<br />', mime_content_type( 'css3menu.zip' ), '<br />';
}
?>
<form enctype="multipart/form-data" action="http://localhost/test.php" method="post">
<input type="file" name="file[]" id="file" />
<input type="submit" name="button" value="Send" />
</form>
The MIME-Type reported by FileInfo and mime_content_type() were the same as before, but the type index in $_FILES returned application/vnd.zend.serverconfig in Linux environment and the less desirable, although valid, application/octet-stream in Windows.
Any ideas of why this is happening?
One thing that worth to mention is I'm new in Linux environment so I didn't know how to setup a webserver so, at first, I installed Zend Server but later I uninstalled it completelly and reinstall everything manually using apt-get.
After understanding that the type index is provided by the browser and not by PHP I tested in Google Chrome and got the correct application/zip
So I Google'd how to fix this issue in Firefox, the browser I was testing before, and all I needed to do was delete the mimeTypes.rdf file in the profile folder located in ~/.mozilla/firefox/