Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a csv into Codeigniter

Has anyone else had trouble uploading a csv file into Codeigniter? I'm getting a pretty annoying "The filetype you are attempting to upload is not allowed." error, even though I've quite explicitly set the upload type. Here's my code (should be fairly standard stuff):

    function doUpload() {

    $config['upload_path'] = 'uploads/';
    $config['allowed_types'] = 'text/plain|text/csv|csv';
    $config['max_size'] = '5000';
    $config['file_name'] = 'upload' . time();

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

    if(!$this->upload->do_upload()) echo $this->upload->display_errors();
    else {
        $file_info = $this->upload->data();
        $csvfilepath = "uploads/" . $file_info['file_name'];
        $this->addfromcsv($csvfilepath);

    }

}

I tried to cover all the bases in my allowed types - maybe I missed one? Thanks for any help with this!

like image 906
StormShadow Avatar asked Jan 19 '11 02:01

StormShadow


2 Answers

I got it working by adding cbrandolino's suggested mimetypes to the config/mimes.php (great tip jljohnstone). So the csv property of my $mimes looks like this now:

'csv'   =>  array('application/vnd.ms-excel', 'text/anytext', 'text/plain', 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel')
like image 59
Jos Faber Avatar answered Sep 22 '22 23:09

Jos Faber


Unluckily there is no official specification, so there's quite a lot of them: the most popular among those that are missing are,

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

It's highly unlikely you'll meet another one.

like image 39
cbrandolino Avatar answered Sep 22 '22 23:09

cbrandolino