【问题标题】:upload pdf, csv and other file type in codeigniter在 codeigniter 中上传 pdf、csv 和其他文件类型
【发布时间】:2015-01-27 21:14:47
【问题描述】:

上传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 ) )

PDF格式

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

$this->upload->display_errors()

两者兼得

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

任何有想法的人都是问题

我也尝试过像这样来自其他 que 的 ans 在 mimes.php 中

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

【问题讨论】:

    标签: php codeigniter file-upload upload


    【解决方案1】:

    请将此添加到您的 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);`
    

    【讨论】:

    • 我丢失了 '$this->upload->initialize($config);'
    • 好的,.fr3 文件是什么文件? pdf是应用程序,html是文本,fr3是?
    • 我不知道。但是您可以打印 $_FILES 并检查 mime 类型。
    【解决方案2】:

    当程序接收多个文件时,我遇到了同样的问题,我看到该方法没有从正在上传的当前文件中重置完整数据。

    分离函数的问题解决了

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

    到:

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

    $this->上传->初始化($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();
        }
    }
    

    因为当我使用 $this->upload->data();一起看

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 2020-02-05
      • 2015-06-04
      • 2013-04-12
      • 2019-02-27
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多