Welcome Guest, Not a member yet? Register   Sign In
Upload class finfo_file returns wrong file type
#1

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 !
Reply
#2

(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?
Reply
#3

Yes sorry, CSV is what I meant.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB