parth parth - 1 month ago 14
PHP Question

upload pdf, csv and other file type in codeigniter

code to upload csv, pdf and other

$config['allowed_types'] = "application/pdf|pdf|application/octet-stream|csv";


on

print_r($_FILES);


gives
for csv

Array ( [userfile] => Array ( [name] => file.csv [type] => application/octet-stream [tmp_name] => C:\xampp\tmp\php4FD2.tmp [error] => 0 [size] => 7 ) )


for pdf

Array ( [userfile] => Array ( [name] => doc.pdf [type] => application/pdf [tmp_name] => C:\xampp\tmp\phpA4E5.tmp [error] => 0 [size] => 127670 ) )


on

$this->upload->display_errors()


gives for both

Array ( [error] => 1 [msg] => The filetype you are attempting to upload is not allowed. )


anyone hving idea wat is the problem

i also tried ans from other que like this
in mimes.php

'pdf' => array('application/pdf', 'application/x-pdf', 'application/x-download','application/x-download', 'binary/octet-stream', 'application/unknown', 'application/force-download'),

Answer

please add this in your application\config\mimes.php file.

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

'pdf' => array('application/pdf', 'application/x-download')

now on upload function in controller use

$config['allowed_types'] = 'pdf|csv';

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