Codeigniter:无法上传文件,未定义索引

时间:2017-04-11 09:36:33

标签: php codeigniter file

我似乎无法在codeigniter中上传文件。我不知道问题是否在于$_FILES,当我尝试做print_r($_FILES)它会显示一个空数组。

附加控制器和下面的视图:

CONTROLLER

public function upload_files(){


    $this->load->library('upload');     
    $filesCount = count($_FILES['userfile']['name']);

    for($i = 0; $i < $filesCount; $i++){
        $pathToUpload="Path/to/folder";
        $folder=$id;
        $uploadPath=$this->create_dir($pathToUpload,$folder);   
        $_FILES['userFile']['name'] = $_FILES['userfile']['name'][$i];
        $_FILES['userFile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
        $_FILES['userFile']['size'] = $_FILES['userfile']['size'][$i];
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = '*';

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

    }
}

查看

<form action="<?php echo base_url(); ?>index.php/Controller_name/upload_files" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-lg-4">Attach Documents</label>
<div class="col-lg-4">
    <input type="file" class="form-control" size="20" name="usefile[]" multiple="multiple" />
</div>
</div> 
</form>

任何帮助将不胜感激。我似乎无法弄清楚为什么$_FILES没有被发布

1 个答案:

答案 0 :(得分:0)

我已使用更正更新了代码。

  1. Remove 1st $this->load->library('upload');
  2. 创建$ _FILES数据的副本。 $files = $_FILES;
  3. 纠正这一行。 $pathToUpload="./Path/to/folder/";
  4. 更正从name="usefile[]name="userFile[]
  5. 的输入字段名称
  6. 更新了视图代码。
  7. 控制器代码:

    public function upload_files(){
    
    
        //$this->load->library('upload');     
        $filesCount = count($_FILES['userfile']['name']);
        $files = $_FILES;
        for($i = 0; $i < $filesCount; $i++){
            $pathToUpload="./Path/to/folder/";
            $folder=$id;
            $uploadPath=$this->create_dir($pathToUpload,$folder);   
            $_FILES['userFile']['name'] = $_FILES['userfile']['name'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
            $_FILES['userFile']['size'] = $_FILES['userfile']['size'][$i];
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = '*';
    
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $this->upload->do_upload('userfile');
            $this->upload->display_errors();
    
        }
    }
    

    查看文件。

    <form action="<?php echo base_url(); ?>index.php/Controller_name/upload_files" method="post" enctype="multipart/form-data">
    <div class="form-group">
    <label class="control-label col-lg-4">Attach Documents</label>
    <div class="col-lg-4">
        <input type="file" class="form-control" size="20" name="usefile[]" multiple="multiple" />
    <input type="hidden" name="key" value="123"/>
    <input type="submit" name="upload" value="Upload"/>
    </div>
    </div> 
    </form>