codeigniter从多个输入上传文件

时间:2017-03-26 12:45:08

标签: codeigniter upload

我想上传多个输入的文件。我已经尝试了下面的代码,但文件未上传,我无法获取上传目录。

HTML

<input type="file" name="userfile[]" size="20" />

控制器

public function detailTugasInput($idKelas,$id){
    $this->load->model('tugass');
    $records = $_FILES['userfile'];
    //var_dump($records); die();
    $config['upload_path']          = './file/';
    $config['allowed_types']        = 'pdf|doc|txt|jpg|png';
    $config['max_size']             = 0;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;

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

    foreach ($_FILES as $key => $value) {
        $upload_data = $this->upload->data();
        $file_ary = array(
        'nama_kembali' => $value['name'],
        'file_kembali' => $value['tmp_name']
        );
    }
}

我还尝试使用下面的代码获取上传的数据,但为什么总是出现错误?

$checked_arr = $_POST['userfile'];

1 个答案:

答案 0 :(得分:0)

尝试以下代码

HTML

<input type="file" class="form-control" name="userfile[]" multiple />

Controller

public function detailTugasInput($idKelas, $id){

    $this->load->model('tugass');

    if(!empty($_FILES['userfile']['name'])) {

        $filesCount = count($_FILES['userfile']['name']);

        for($i = 0; $i < $filesCount; $i++) {

            $_FILES['userFile']['name'] = $_FILES['userfile']['name'][$i];
            $_FILES['userFile']['type'] = $_FILES['userfile']['type'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
            $_FILES['userFile']['error'] = $_FILES['userfile']['error'][$i];
            $_FILES['userFile']['size'] = $_FILES['userfile']['size'][$i];

            $config['upload_path'] = './file/';
            $config['allowed_types'] = 'pdf|doc|txt|jpg|png';

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

            $this->upload->do_upload('userFile');
            if($this->upload->do_upload('userFile')){
                $fileData = $this->upload->data();
                $uploadData[$i]['file_name'] = $fileData['file_name'];
                $uploadData[$i]['created'] = date("Y-m-d H:i:s");
                $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
            }

        }
    }
}

如需更多支持,Check This