php 上传带有 codeigniter、mime 类型错误的 xls 或 xlsx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9166892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload xls or xlsx files with codeigniter, mime-type error
提问by Mario Cesar
Well, i believe this is not a Codeigniter problem per seas it is more of a mime-type.
好吧,我相信这本身不是 Codeigniter 问题,因为它更像是一个mime-type。
I'm trying to upload a file, a xls (or xlsx) file and the mime-type the browser and the php report is application/octet-streaminstead of application/excel, application/vnd.ms-excelor application/msexcelfor a xls file. Of course codeigniter upload plugin will report an error (invalid file type) as it tries to match the file extension with the mime-type.
我正在尝试上传文件、xls(或 xlsx)文件和浏览器的 MIME 类型,php 报告是application/octet-stream而不是application/excel、application/vnd.ms-excel或application/msexcel对于 xls 文件。当然,codeigniter 上传插件会报告错误(文件类型无效),因为它尝试将文件扩展名与 mime 类型匹配。
The weird(est) thing might be that the same code worked for months and now stopped working with the latest Chrome (16.0.912.77), Firefox (10.0) and IE9.
奇怪的(最)事情可能是相同的代码工作了几个月,现在停止与最新的 Chrome (16.0.912.77)、Firefox (10.0) 和 IE9 一起工作。
Has anyone had the same (or similar) problem and care to share the solution?
有没有人有相同(或类似)的问题并愿意分享解决方案?
Thank you very much. PS: I won't provide code as it's not really a code matter, but if necessary i'll upload some snippets.
非常感谢。PS:我不会提供代码,因为它不是真正的代码问题,但如果有必要,我会上传一些片段。
EDIT
编辑
It might be relevant: the error doesn't happen with same browsers on a similar configuration, but with MS Office instead of Libre Office (on my pc). It doesn't happen on a GNU/Linux based + Libre Office system either. SO, could it be Windows playin' hard on the open source suite, or the Libre Office changing the mime-types just for the heck of it?
这可能是相关的:错误不会发生在类似配置的相同浏览器上,而是使用 MS Office 而不是 Libre Office(在我的电脑上)。它也不会发生在基于 GNU/Linux 的 + Libre Office 系统上。那么,是 Windows 在开源套件上大打折扣,还是 Libre Office 只是为了好玩而改变了 mime 类型?
采纳答案by Stevo
I'm getting this error also.
我也收到这个错误。
CI is reporting a file type of 'application/zip' which makes sense as the xlsx format is a compressed format (rename it to zip and you can open the contents).
CI 报告的文件类型为“应用程序/zip”,这是有道理的,因为 xlsx 格式是一种压缩格式(将其重命名为 zip,您可以打开内容)。
I have added/replaced the following line to the mime types file (application/config/mimes.php):
我在 mime 类型文件 (application/config/mimes.php) 中添加/替换了以下行:
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/zip'),
and that works (for this browser at least!)
这有效(至少对于这个浏览器!)
回答by Randika Vishman
Please, go through the following Description and the hint and get the Answer easily!
请仔细阅读以下说明和提示,轻松获得答案!
Description:
描述:
Actually as many ones have advised to add/replace the following line in the file (application/config/mimes.php):
实际上,许多人建议在文件 (application/config/mimes.php) 中添加/替换以下行:
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
But I have realized that in CodeIgniter Version 2.2.*the issue is little bit different!
They have added that line already, but forgot to add the following "file_type" ==> 'application/vnd.ms-excel'
但我意识到在CodeIgniter 版本 2.2.* 中,问题有点不同!他们已经添加了该行,但忘记添加以下“file_type” ==> 'application/vnd.ms-excel'
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel'),
So adding the above 'application/vnd.ms-excel'into the array of xlsxfile type, let me upload the .xlsxfiles!
所以将上面的'application/vnd.ms-excel'加入到xlsx文件类型的数组中,让我上传.xlsx文件!
Hint:
暗示:
Whenever you get the following error, on CodeIgniter Platform, and uploading files:
每当您在 CodeIgniter 平台上遇到以下错误并上传文件时:
The filetype you are attempting to upload is not allowed.
不允许您尝试上传的文件类型。
Do the following in your Controller's upload method,
在控制器的上传方法中执行以下操作,
var_dump($this->upload->data());
And it will give you a huge array which you can get an idea from this link.(Please, see the end of that page). In that array you can get what's the real mime_type of the file you are trying to upload but not giving you to upload.
它会给你一个巨大的数组,你可以从这个链接中得到一个想法。(请参阅该页面的末尾)。在该数组中,您可以获得您尝试上传但不让您上传的文件的真实 mime_type 是什么。
Answer:
回答:
In my case, my file extension was, .xlsx, and the mime type was application/vnd.ms-excel, which was not added into the
就我而言,我的文件扩展名是.xlsx,而 mime 类型是application/vnd.ms-excel,它没有添加到
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
So I added it manually, and after that it works VERRY WELL!!!
所以我手动添加了它,之后它工作得非常好!!!
Same thing happened to uploading CSV once again, when I checked the file extension is .csvbut the mime type was text/plain, when I added it to the following line:
再次上传 CSV 时发生了同样的事情,当我检查文件扩展名是.csv但 mime 类型是text/plain 时,当我将其添加到以下行时:
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel');
and saved as follows,
并保存如下,
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'),
It works like a charm! :D Try it, if you find something new within the above steps, please comment here!!! So, hoping this will be helpful to all of the CodeIgniter community, I posted it taking some time!
它就像一个魅力!:D 试试吧,如果你在上面的步骤中发现了一些新东西,请在这里评论!!!所以,希望这对所有 CodeIgniter 社区都有帮助,我花了一些时间发布它!
Best Regards guys,
最好的问候,伙计们,
Randika
兰迪卡
回答by user2979425
Just for the records, I found the cause, mime-type was missing on the windows registry, solved adding these keys with a .reg file:
只是为了记录,我找到了原因,windows 注册表中缺少 mime-type,解决了使用 .reg 文件添加这些键的问题:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\.xls]
"Content Type"="application/excel"
[HKEY_CURRENT_USER\Software\Classes\.xlsx]
"Content Type"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
But would rather use the solutions above, I don't like to mess with the registry.
但是宁愿使用上面的解决方案,我不喜欢弄乱注册表。
回答by Seabass
This was a CI bug a few months ago: https://github.com/EllisLab/CodeIgniter/issues/394. mimes.php in the framework was updated and the bug was resolved. Update your CodeIgniter library to 2.1.0 (or newer).
这是几个月前的 CI 错误:https: //github.com/EllisLab/CodeIgniter/issues/394。更新了框架中的 mimes.php 并解决了错误。将您的 CodeIgniter 库更新到 2.1.0(或更新版本)。
Also a good thing to test/dump are your server mime types.
测试/转储的另一个好处是您的服务器 mime 类型。
Another alternative is forcing the mime type. With .htaccess, that would be
另一种选择是强制使用 mime 类型。使用 .htaccess,那就是
AddType application/excel .xls .xlsx
For a whole debugging adventure, test various office files with get_mime_by_extension($file)
with the File Helper (http://codeigniter.com/user_guide/helpers/file_helper.html)
对于整个调试冒险,get_mime_by_extension($file)
使用文件助手测试各种办公文件(http://codeigniter.com/user_guide/helpers/file_helper.html)