在codeigniter中上传pdf,csv和其他文件类型

时间:2014-11-29 11:31:51

标签: php codeigniter file-upload upload

上传csv,pdf和其他

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

print_r($_FILES);

给出 对于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()

同时给出了

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

任何想法的人都是问题

我也尝试过像这样的其他问题 在mimes.php

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

2 个答案:

答案 0 :(得分:4)

请在您的application \ config \ mimes.php文件中添加。

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

现在在控制器使用中的上传功能

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

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

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

答案 1 :(得分:0)

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