Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

upload - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...