我似乎无法在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
没有被发布
答案 0 :(得分:0)
我已使用更正更新了代码。
Remove 1st $this->load->library('upload');
$files = $_FILES;
$pathToUpload="./Path/to/folder/";
name="usefile[]
到name="userFile[]
控制器代码:
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>