在cpanel中上传csv文件时的codeigniter问题

时间:2015-05-07 10:49:44

标签: php codeigniter csv file-upload

我得到以下错误: 您尝试上传的文件类型是不允许的。 我想上传csv文件。

我使用codeigniter文件上传方法do_upload 我还提供allowed_types作为csv

public function csvRateList(){
$redirectData=['error'=>'','success'=>''];

$type=$this->input->post('type');

date_default_timezone_set('Asia/Kolkata');

$config['upload_path'] ='./csv/';

$config['allowed_types'] = 'csv'; //type of file

$config['max_size'] = '100';

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

$query = $this->db->get_where('csv_rate_list', array('type' => $type));

    if($query->num_rows()==0){
        $query = $this->db->get_where('rate_list', array('type' => $type));
            if($query->num_rows()==0){
            if($this->upload->do_upload()){
                $fdata=$this->upload->data();
                $newName=$fdata['file_name'];
                $origName=$fdata['orig_name'];
                $data = array(
                'type'      => $type ,
                'new_name'  => $newName ,
                'orig_name' => $origName,
                'timestamp' =>time()
                );
                $this->db->insert('csv_rate_list', $data); 
            }else{
                $redirectData['error']=$this->upload->display_errors();
                redirect(base_url().'add_rate');
            }
                $redirectData['success']='Successfully inserted!';
                $this->session->set_flashdata($redirectData);
                redirect(base_url().'add_rate');
            }else{
                $redirectData['error']='Service type already exists. in old table';
                $this->session->set_flashdata($redirectData);
                redirect(base_url().'add_rate');
            }
    }else{
        $record=$query->row_array();
        $id=$record['id'];
        $old_name=$record['new_name'];
        if($this->upload->do_upload()){
            $fdata=$this->upload->data();
            $newName=$fdata['file_name'];
            $origName=$fdata['orig_name'];
            $data = array(
            'type'      => $type ,
            'new_name'  => $newName ,
            'orig_name' => $origName,
            'timestamp' =>time()
            );
            $this->db->where('id', $id);
            $this->db->update('csv_rate_list', $data); 
            unlink('./csv/'.$old_name);
            $redirectData['success']='Successfully updated!';
            $this->session->set_flashdata($redirectData);
            redirect(base_url().'add_rate');
        }else{
            $redirectData['error']=$this->upload->display_errors();
            $this->session->set_flashdata($redirectData);
            redirect(base_url().'add_rate');
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你必须修改几行(/system/libraries/Upload.php)

从:

$this->file_type = @mime_content_type($file['tmp_name']);
return;

到此:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 

答案 1 :(得分:0)

检查config / mimes.php是否为" csv"

具有正确的值

在$ mimes数组中,找到' csv'并检查以下内容。

' CSV' =>数组(' text / x-逗号分隔值',' text /逗号分隔值',' application / octet-stream',' ; application / vnd.ms-excel',' text / x-csv',' text / csv',' application / csv',&#39 ; application / excel',' application / vnd.msexcel'),

如果没有,请将其添加到其中。