1

We are getting an error

Error:

Error: The filetype you are attempting to upload is not allowed.

 array(1) { ["csv"]=> array(5) { ["name"]=> string(18) "cod_sp_payment.csv" ["type"]=> string(24) "application/vnd.ms-excel" ["tmp_name"]=> string(24) "C:\xampp\tmp\php5910.tmp" ["error"]=> int(0) ["size"]=> int(155) } }

Controller Code:

$config['upload_path'] = './uploads/excel/';
$config['allowed_types'] = 'text/comma-separated-values|application/csv|application/excel|application/vnd.ms-excel|application/vnd.msexcel|text/anytext|text/plain|text/csv|csv';
$this->load->library('upload', $config);

if(! $this->upload->do_upload('csv')) {

    $error = array('error' => $this->upload->display_errors());

    // $this->load->view('upload_form', $error);

    echo $this->upload->display_errors();
}

$upload_data = $this->upload->data();

exit();
Anu Abraham
  • 43
  • 1
  • 6

3 Answers3

1

The $config['allowed_types'] = 'csv'; may not do the trick always. You may need to add some more types like below

text/comma-separated-values|application/csv|application/excel|application/vnd.ms-excel|application/vnd.msexcel|text/anytext|text/plain|text/csv|csv

In your answer you can find

[type] => application/vnd.ms-excel 

The mime type is ms-excel. So CSV won't work. And we ca't specify the mimtype of a csv file due to this different mime type

**Note:**There is another error in your code

Message: fopen(test2/uploads/excel/cod_sp_payment.csv): failed to open stream: No such file or directory

Please check the file is exists or not

Arun
  • 3,640
  • 7
  • 44
  • 87
  • @AnuAbraham but hard to under stand code in comments you can always update your question by clicking on edit button below tags. –  Jul 12 '16 at 08:49
  • @ Arun we have changed our code as you suggested. Please check the above code – Anu Abraham Jul 12 '16 at 09:06
0

please confirm whether your config/mimes.php has the filetype included. To know the filetype of the uploaded file, do the following:

if(! $this->upload->do_upload('csv'))
{
    $error = array('error' => $this->upload->display_errors());
    // $this->load->view('upload_form', $error);
    echo $this->upload->data();
    echo $this->upload->display_errors();
}
$upload_data = $this->upload->data();
Jisha
  • 55
  • 1
  • 7
0

This happens because different servers treat files differently. It can be the same file but the mime type won't be seen as the same.

The solution:

  1. On CodeIgniter, open the file system/libraries/Upload.php. On line 199 (in CI ver.3.x), change the line $this->file_size = $_FILES[$field]['size']; to $this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();

  2. Upload the file to the server to test. Run the test. Identify your file type. Copy the file type to be used again later.

  3. Now remove the change in step 1 above and re-upload the file.

  4. Now open the file application/config/mimes.php. Go to line 97. It's the part that has the mime type for 'xlsx'. Include your new mime type by pasting it here. Upload the file to your server.

Done. Now your application/upload should work.

I just found a similar answer to this here: CodeIgniter: "The filetype you are attempting to upload is not allowed."

It seems more detailed.

itsols
  • 5,406
  • 7
  • 51
  • 95