CodeIgniter: wrong uploading file mime type

﹥>﹥吖頭↗ 提交于 2019-12-25 05:14:54

问题


Sometimes, when uploading files using <input> tag, I encounter problem where the file extension doesn't match with its mime type defined in application/config/mimes.php. For example when I upload a .doc file, it turns out that its mime type is actually application/octet-stream, not application/msword as expected.

I had this problem sometimes in the past. I did work around by adding application/octet-stream to .doc mime array. But is there a proper way to fix it?

I think the browser have force my file's mime type somehow. Is there any kind of HTTP header or html meta tag to prevent this?


回答1:


As you can see here this a common problem and the solution you applied is legitimate.




回答2:


It is not due to problem with codeignitor. It is due to php issue. For instance finfo_file function will return image with extension jpeg as image/png.

So, solution is go to config/mimes.php file and then add corresponding extension.

In case you do not know what is the extension behind the scene php is picking add this code to check.

$finfo = @finfo_open(FILEINFO_MIME);
$mime = @finfo_file($finfo, $_FILES["myname"]['tmp_name']);

$mime will be extension which php finally returns.



来源:https://stackoverflow.com/questions/10762391/codeigniter-wrong-uploading-file-mime-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!