Codeigniter 3 中的文件上传

File upload in Codeigniter 3

我尝试使用 codeigniter 上传 wav 文件。 但是我收到了这个错误信息;
The filetype you are attempting to upload is not allowed.

代码如下:

    $config['upload_path'] = getwdir() . 'voices/';
    $config['allowed_types'] = 'wav|mp3';
    $config['max_size'] = 2800000;
    $config['file_name'] = rand();
    $this->upload->initialize($config);
    var_dump($config);
    if ($this->upload->do_upload('file')) {
        var_dump('uploaded');
    }else{
        var_dump($this->upload->display_errors());
    }

var_dump($_FILES['file']);

  array (size=1)
 'file' => 
    array (size=5)
      'name' => string 'blob' (length=4)
      'type' => string 'audio/wav' (length=9)
      'tmp_name' => string '/tmp/phpe2SQi5' (length=14)
      'error' => int 0
      'size' => int 98348

使用具有这些配置值的上传库时:

$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'wav';
$config['max_size'] = '1000000';
$this->load->library('upload', $config).

如果您尝试上传 .wav 文件(已在 firefox 7.0.1 中测试)。 会报错'The filetype you are attempting to upload is not allowed.'.

This can be solved by replacing line 68 in application/config/mimes.php

'wav' => 'audio/x-wav'
 with:
'wav' => array('audio/wav', 'audio/wave', 'audio/x-wav'),

好的,如果您已经尝试过,那么试试这个:

您可以尝试查看 system/libraries/Upload.php 第 199 行:

$this->_file_mime_type($_FILES[$field]);

改为

$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();

然后上传您的 .wmv 文件。它会显示类似 application/octet-stream 之类的内容。将其添加到您的 mimes.php.

一些Userfull链接:

link1 , Link2

这对你有帮助

查看HTML

<form method="POST" enctype="multipart/form-data" action="/your_controller/do_upload" >
<input type="file" name="fileForUpload">
<input type="submit" value="Upload">

控制器

    <?php 
public function do_upload(){
    $config = array();
    $config['upload_path'] = './path_from_root_to_dir/';
    $config['allowed_types'] = '*'; //'gif|jpg|png';
    $config['encrypt_name']  = TRUE;
    //$config['max_size'] = 100;
    //$config['max_width'] = 1024;
    //$config['max_height'] = 768;
    $this->load->library('upload',$config);
    if ( ! $this->upload->do_upload('fileForUpload')) {
        $error = array('error' => $this->upload->display_errors());
        //Action, in case file upload failed
    } else {
        //Action, after file successfully uploaded
    }
}