Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...