简体   繁体   中英

xls file is not uploading in codeigniter?

I want to upload csv file and xls file, my code has given below
`

        `$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".

Result of print_r($_FILES) is

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'   =>  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

 $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. Answer suggests that browser sends xls(x) as application/zip.

Please refer to this topic: Upload xls or xlsx files with codeigniter, mime-type error

Sollution:

I have added/replaced the following line to the mime types file (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. You can determine your file mime type using data() method in upload file calss

Please check the following :

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

The xls or xlsx,csv problem is with the mime type.

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

var_dump($this->upload->data()); So, The type of mime will be identified. Copy the output array by index name file_type.

'file_type' => string 'application/vnd.ms-office' (length=25)

Then open the mimes.php in application/config/ folder.

Search for the xls, xlsx and add the following mime type for the array.

Maximum it covers all the mime types.

'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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