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 t开发者_开发技巧his!
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')
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.
I had the same problem uploading .csv files. I solved it by determining the mime type by using file -I file.csv
in the OS X Terminal. It reported that the mime type was 'text/plain' so I added that to the config/mimes.php file.
-- Codeigniter 2.2.0 --
I did a quick debug on the Upload library and I have found out that sometimes the same source code running on different servers will identify the same CSV file having different file type (i.e. Server 1 identify the CSV file as: application/vnd.ms-excel but Server 2 identify the same CSV file as: text/x-c).
My fix was to add, in application/config/mimes.php, the file type that is identified by the server where was the issue. Hence, my csv array from mimes.php file looks like:
'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', 'text/x-c')
.
first in allowed types just type 'csv'
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
second go to /config/mimes.php and change the 'csv' array element as follows:
'csv' => array(
'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',
'text/plain'
),
I tried many things to upload csv file in codeigniter:-
i add mime type ( application/vnd.ms-excel ) to mime type file but the mean issue is codeigniter will only accept file name userfile. if you will set file name userfile then it works.
I think there might be another way but i unable to find that.
精彩评论