CodeIgniter Forums
Upload class finfo_file returns wrong file type - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Upload class finfo_file returns wrong file type (/showthread.php?tid=63709)



Upload class finfo_file returns wrong file type - Arcane - 12-01-2015

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 !


RE: Upload class finfo_file returns wrong file type - skunkbad - 12-01-2015

(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?


RE: Upload class finfo_file returns wrong file type - Arcane - 12-01-2015

Yes sorry, CSV is what I meant.