unable to upload an excel file in codeigniter

我怕爱的太早我们不能终老 提交于 2019-12-11 12:57:31

问题


I have a Codeigniter project that can insert data into database through uploading an excel file and reading it with PHPExcel. It is working on my localhost however when I uploaded it in a LAMP server, It gives me an error The filetype you are attempting to upload is not allowed. How am I going to solve this?

EDIT In my project, I also have a file image uploading..

 public function uploadConfig(){
    $this->load->library('PHPExcel');  
    $this->load->library('PHPExcel/IOFactory');
    $config['upload_path'] = './csv_uploads/'; 
    $config['allowed_types'] = 'xlsx|csv|xls';
    $config['max_size'] = '10000'; 
    $config['overwrite'] = true;
    $config['encrypt_name'] = FALSE;
    $config['remove_spaces'] = TRUE;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if (!is_dir('csv_uploads'))
    {
        mkdir('./csv_uploads', 0777, true);
    }
    // gif|jpg|jpeg|png
}

This works on my localhost in XAMPP. However, when I uploaded it on my LAMP server, it didn't work.


回答1:


For The file type you are attempting to upload is not allowed this issue occur because of your file type is wrong or not fulfill.

Please apply below step to get your file type.

You can looking at system/libraries/Upload.php line 199:

$this->_file_mime_type($_FILES[$field]);

Update that line to:

 $this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();

Now you can see your actual file type Like xlsx or csv or xls or ods

Now you can add your file type below code

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

also add in config/mimes.php




回答2:


Codeigniter 3

add to your config/mimes.php types 'application/octet-stream'

'xlsx'  =>  array('application/octet-stream','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel', 'application/msword', 'application/x-zip')

then it will work




回答3:


Go to config/mimes.php -> lines no 32 (xls) -> add 'application/vnd.ms-office' into existing array.

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

This work only for xls for any other type get the mime type and add into this array.




回答4:


If you are using liberOffice or Open Office, You will face this problem. add "application/octet-stream" type in 'xlsx' to your application/config/mimes.php

'xlsx'  =>  array('application/octet-stream','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel', 'application/msword', 'application/x-zip')



回答5:


In your code

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

But in Ubuntu it save file types is ods format. So it will never let allow to upload. add that

$config['allowed_types'] = 'xlsx|csv|xls|ods';

And in config/mimes.php

add this

'ods'   =>  array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),

Provide Permeation to write in the folder

sudo chown apache:apache -R /var/www/html

and in php.ini

  1. Check file_upload is on
  2. and check max upload limit
  3. as well as check max execution time



回答6:


In Your Code Add Below Line

 $config['allowed_types']        = '*';
 $config['detect_mime']          = false;

this is configuration setting.



来源:https://stackoverflow.com/questions/33427906/unable-to-upload-an-excel-file-in-codeigniter

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