I have the following PHP code that shows the mime type of an uploaded file.
<?php
if ($_POST) {
var_dump($_FILES);
$finfo = new finfo(FILEINFO_MIME_TYPE);
var_dump($finfo->file($_FILES['file']['tmp_name']));
} else{
?>
<form method="POST" enctype="multipart/form-data"><input name="file" type="file"><input name="submit" value="send" type="submit"/></form>
<?php
}
The result of uploading somefile.csv with this script is as follows.
array (size=1)
'file' =>
array (size=5)
'name' => string 'somefile.csv' (length=12)
'type' => string 'text/csv' (length=8)
'tmp_name' => string '/tmp/phpKiwqtu' (length=14)
'error' => int 0
'size' => int 3561
string 'text/x-fortran' (length=14)
So of course the mime type should be text/csv. But the framework I use (Symfony 1.4) uses the method with fileinfo.
Also I tested a little further it seems that the command (on Ubuntu) file --mime-type somefile.csv
returns somefile.csv: text/x-fortran
and the command mimetype somefile.csv
returns somefile.csv: text/csv
. somefile.csv is created with MSOffice (I don't know if this matters).
Apparently mimetype
uses some awesome mime database (http://freedesktop.org/wiki/Software/shared-mime-info), while file
does not.
- Does PHP use
file
ormimetype
or neither? - Further, I am not sure what to do here; is my uploaded file wrongly formatted? Do I have to use a different mime database? Is PHP bugged? What is going on here?
edit:
The reason why it is detected as a fortran program is because somefile.csv contains only the following:
somecolumn;
C F;
I believe the above contents of a CSV file is valid right? If a field contains a space this field does not have to be put inside quotes, right?