upload pdf, csv and other file type in codeigniter

♀尐吖头ヾ 提交于 2019-12-13 05:26:12

问题


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

回答1:


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);`



回答2:


I had the same problem when the program receives multiples files, i saw that the method not resets the complete data from the current file that is uploading.

The problem was solved separating the function

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

to:

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

$this->upload->initialize($config);

function fn_upload_file($file_name, $path, $types = 'gif|jpg|jpeg|png|mp3|mpeg') {
    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = $types;
    $config['max_size']    = '15000000';

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

    $this->upload->initialize($config);

    if(!is_dir($path)){
        @mkdir($path, 0777, true);
    }

    if ( ! $this->upload->do_upload($file_name)) {
        throw new Exception($this->upload->display_errors());
    } else {
        return $this->upload->data();
    }
}

Cause when i used the $this->upload->data(); to see with



来源:https://stackoverflow.com/questions/27201741/upload-pdf-csv-and-other-file-type-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!