在Codeigniter 3中上传文件

在Codeigniter 3中上传文件

问题描述:

i try to upalod wav file using codeigniter. But i have get this error message;
The filetype you are attempting to upload is not allowed.

Code Shown below:

    $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

This will help you

View HTML

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

Controller

    <?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
    }
}

When using the upload library with these config values:

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

If you attempt to upload a .wav file (tested in firefox 7.0.1). It will report an error of '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'),

Ok If you Tried it already then try this:

You could try looking at system/libraries/Upload.php line 199:

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

Change to

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

Then do upload your .wmv file. It would show something like application/octet-stream or whatever. Add that to your mimes.php.

Some Userfull links:

link1 , Link2