简体   繁体   English

xls文件未在codeigniter中上传?

[英]xls file is not uploading in codeigniter?

I want to upload csv file and xls file, my code has given below 我想上传csv文件和xls文件,我的代码如下
` `

        `$configUpload['upload_path'] = './user_status/';`
         $configUpload['allowed_types'] = 'XLS|text/comma-separated-values|application/csv|application/excel|application/vnd.ms-excel|application/vnd.msexcel|text/anytext|text/plain|text/csv|csv|application/vnd.ms-excel';
         $configUpload['max_size'] = '5000';
         $this->load->library('upload', $configUpload);
         $this->upload->do_upload('input field name')`;

my csv file is uploading very well but when I choose xls file then codeigniter show error that "The filetype you are attempting to upload is not allowed". 我的csv文件上传得很好,但是当我选择xls文件时,codeigniter显示错误“不允许您尝试上传的文件类型”。

Result of print_r($_FILES) is print_r($ _ FILES)的结果是

Array ( [user_status_csv] => Array ( [name] => VTRACK.XLS [type] => application/vnd.ms-excel [tmp_name] => C:\xampp\tmp\phpB2C4.tmp [error] => 0 [size] => 2627412 ) ) 

I had the same problem. 我有同样的问题。 and i solved by modifying the mime for xls 我通过修改xls的mime解决了

'xls'   =>  array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),

to

array('application/excel', 'application/vnd.ms-excel', 'application/octet-stream'),

You can also try XLSX 您也可以尝试XLSX

 $configUpload['allowed_types'] = '**XLSX|**XLS|text/comma-separated-values|application/csv|application/excel|application/vnd.ms-excel|application/vnd.msexcel|text/anytext|text/plain|text/csv|csv|application/vnd.ms-excel';

Also you can just give the file extensions as this 你也可以这样给文件扩展名

 $configUpload['allowed_types'] = 'xls|xlsx|csv';

You can try to use sollution from similair topic. 您可以尝试使用similair主题中的解决方案。 Answer suggests that browser sends xls(x) as application/zip. 答案表明浏览器将xls(x)作为application / zip发送。

Please refer to this topic: Upload xls or xlsx files with codeigniter, mime-type error 请参考此主题:上载带有codeigniter,mime类型错误的xls或xlsx文件

Sollution: Sollution:

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'),

You have to set the mime type in mimes.php configuration file. 您必须在mimes.php配置文件中设置mime类型。 You can determine your file mime type using data() method in upload file calss 您可以在上传文件校准中使用data()方法确定文件的MIME类型

Please check the following : 请检查以下内容:

http://isaber.info/blog/2013/01/23/codeigniter-upload-files-not-work/ http://isaber.info/blog/2013/01/23/codeigniter-upload-files-not-work/

The xls or xlsx,csv problem is with the mime type. xls或xlsx,csv问题与mime类型有关。

The solution is: 解决方案是:

if ( $this->upload->do_upload('filename') ){
       $img = $this->upload->data();
       $ext = $img['file_ext'];                            
       $post['xlfile'] = time().$ext;
} else {
       var_dump($this->upload->data());
       exit();                            
       redirect('hr/hr/dashboard/');
}

If the excel file failed to upload, then in the else condition write 如果excel文件上传失败,则在else条件下写入

var_dump($this->upload->data()); 的var_dump($这 - > upload->数据()); So, The type of mime will be identified. 因此,将确定MIME类型。 Copy the output array by index name file_type. 通过索引名称file_type复制输出数组。

'file_type' => string 'application/vnd.ms-office' (length=25) 'file_type'=>字符串'application / vnd.ms-office'(长度= 25)

Then open the mimes.php in application/config/ folder. 然后在application / config /文件夹中打开mimes.php。

Search for the xls, xlsx and add the following mime type for the array. 搜索xls,xlsx并为数组添加以下mime类型。

Maximum it covers all the mime types. 最大的覆盖了所有的MIME类型。

'xls' =>       array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/x-zip', 'application/vnd.ms-excel', 'application/msexcel','application/excel','application/vnd.ms-office'),

This will work like charm. 这将像魅力一样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM