Codeigniter issue when loading csv file in cpanel

I am getting the following error: The file type you are trying to download is not allowed. I want to download a csv file.

I am using the codeigniter do_upload file upload method and I also supply allowed_types as 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');
        }
    }
}

      

+3


source to share


2 answers


You need to change a few lines in (/system/libraries/Upload.php)

from

$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; 

      

+2


source


Check that config / mimes.php has the correct value for "csv"

Find "csv" in the $ mimes array and check the following.



'csv' => array ("text / x-comma-separated-values", "text / comma-separated-values", "application / octet-stream", "application / vnd.ms-excel", 'text / x-csv ',' text / csv ',' application / csv ',' application / excel ',' application / vnd.msexcel '),

If not, add this to it.

0


source







All Articles