3

I'm getting following Error: The filetype you are attempting to upload is not allowed. i want to upload csv file.

i'm using codeigniter file upload method do_upload 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');
        }
    }
}
8
  • have your code enter into if($query->num_rows()==0){ loop?? Commented May 7, 2015 at 10:57
  • check with condition $query->num_rows() > 0 Commented May 7, 2015 at 10:58
  • This code is working fine on my local server xampp! There is only one problem with file type "csv" in cpanel Commented May 7, 2015 at 11:00
  • check permission of upload folder Commented May 7, 2015 at 11:01
  • any type of error it show?? Commented May 7, 2015 at 11:11

2 Answers 2

2

You have to modify few line in (/system/libraries/Upload.php)

from:

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

to this:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 
Sign up to request clarification or add additional context in comments.

Comments

0

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

In $mimes array,find the 'csv' 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 into it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.