CodeIgniter Forums

Full Version: Upload class finfo_file returns wrong file type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Today, I was trying to upload a CVS file using the Upload Class on one of my website and kept getting a filetype error message, although the MIME type of my file (text/csv) was properly configured in the accepted types.
After some investigation, I have discovered that the following block of code from the Upload class was returning a wrong MIME type (text/x-c++) :

Code:
if (function_exists('finfo_file'))
{
$finfo = @finfo_open(FILEINFO_MIME);
if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
{
$mime = @finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);

/* According to the comments section of the PHP manual page,
* it is possible that this function returns an empty string
* for some files (e.g. if they don't exist in the magic MIME database)
*/
if (is_string($mime) && preg_match($regexp, $mime, $matches))
{
$this->file_type = $matches[1];
return;
}
}
}

Further googleing indicates that finfo_file may return inappropriate mime types.

I had no other solution but to disable this method of detection.

Any possible workaround?

Thanks in advance !
(12-01-2015, 10:37 AM)Arcane Wrote: [ -> ]Hello,

Today, I was trying to upload a CVS file using the Upload Class on one of my website and kept getting a filetype error message, although the MIME type of my file (text/csv) was properly configured in the accepted types.
After some investigation, I have discovered that the following block of code from the Upload class was returning a wrong MIME type (text/x-c++) :

Code:
if (function_exists('finfo_file'))
{
$finfo = @finfo_open(FILEINFO_MIME);
if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
{
$mime = @finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);

/* According to the comments section of the PHP manual page,
* it is possible that this function returns an empty string
* for some files (e.g. if they don't exist in the magic MIME database)
*/
if (is_string($mime) && preg_match($regexp, $mime, $matches))
{
$this->file_type = $matches[1];
return;
}
}
}

Further googleing indicates that finfo_file may return inappropriate mime types.

I had no other solution but to disable this method of detection.

Any possible workaround?

Thanks in advance !

Maybe you mean CSV? A CSV is comma delimited text. I don't know what a CVS is, but maybe that is why you are having problems? File extension is wrong?
Yes sorry, CSV is what I meant.