使用Codeigniter上传多个文件[错误:您没有选择要上传的文件。]

时间:2015-02-10 04:58:51

标签: php codeigniter file-upload

我正在尝试上传多个文件,我想用当前时间重命名每个文件。但是当我上传文件时,我得到的错误就像

  

您没有选择要上传的文件。

我真的不知道我哪里出错了。

非常感谢任何帮助。谢谢..

这是我的控制器:

public function upload_form()
{
    //load the helper
    $this->load->helper('form');
    $msg = array('msg' => "");
    if (isset($_FILES['upload_file'])) {
        $class = $this->input->post('class');
        $subject = $this->input->post('subject');
        $topic = $this->input->post('topic');

        $msg = array('msg' => "Upload success!");
        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'pdf|jpg|png|jpeg|gif';
        $this->load->library('upload', $config);
        $files = $_FILES;
        $cpt = sizeof($_FILES['upload_file']['name']);

        if(!$this->upload->do_upload('upload_file')){
            $msg = array('msg' => $this->upload->display_errors());
        }
        else{
            $data = array(
                'class' => $class,
                'subject' => $subject,
                'topic' => $topic,
                'file' => [
                    'file_name' => $_FILES['upload_file']['name']
                ]
            );
            $this->mongo_db->insert('file_upload', $data);
        }

        for ($i = 0; $i < $cpt; $i++) {
            $this->upload->initialize($config);
            $file_name = (microtime(true) * 1000);
            $_FILES['upload_file']['name'] = $file_name;
            $_FILES['upload_file']['type'] = $files['upload_file']['type'][$i];
            $_FILES['upload_file']['tmp_name'] = $files['upload_file']['tmp_name'][$i];
            $_FILES['upload_file']['error'] = $files['upload_file']['error'][$i];
            $_FILES['upload_file']['size'] = $files['upload_file']['size'][$i];
        }
    }

    $this->load->view('worksheets/header');
    $this->load->view('worksheets/upload_form', $msg);
}

查看:

 <?php echo $msg; ?>
 <?php echo form_open_multipart('worksheets/upload_form/'); ?>
 <input type="file" name="upload_file[]" multiple="multiple" class="custom-     file-input" size="2000"/>
 <input type="submit" value="Fire" class="upload_submit"/>
 <?php echo form_close(); ?>

2 个答案:

答案 0 :(得分:0)

尝试打印$ _FILES [&#39; upload_file&#39;]的结果,您会发现错误。

答案 1 :(得分:0)

基本上,Codeigniter文件上传库不适用于使用<input type="file" name="upload[]" multiple>

进行多文件上传

您需要多个文件输入,因为它只将输入的名称作为字符串。

或者您可以扩展库以进行多个文件上传。

你可以使用这个&gt; https://github.com/stvnthomas/CodeIgniter-Multi-Upload