在codeigniter中上传多个文件,并使用数组重命名每个文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在codeigniter中上传多个文件,并使用数组重命名每个文件相关的知识,希望对你有一定的参考价值。
我有多个上传控制器,它工作正常,但我想在上传时使用一个控制器重命名每个文件。
我想点击提交时,第一个文件名是IKPI,第二个名字是NPWP
<div class="col-sm-6">
<div class="alert alert-block alert-danger">
<p>
<b>IKPI</b>
</p>
<p>
<div class="col-xs-12">
<input type="file" id="id-input-file-2" name="multipartFiles[]" />
</div>
<input type="submit" name="submit" value="Upload File" class="btn btn-sm btn-primary">
</p>
</div>
</div>
<div class="col-sm-6">
<div class="alert alert-block alert-danger">
<p>
<b>NPWP</b>
</p>
<p>
<div class="col-xs-12">
<input type="file" id="id-input-file-2" name="multipartFiles[]" />
</div>
<input type="submit" name="submit" value="Upload File" class="btn btn-sm btn-primary">
</p>
</div>
</div>
在我的控制器中我使用循环
for ($i=0; $i<$number_of_files ; $i++) {
$_FILES['multipartFiles']['name'] = $files['multipartFiles']['name'] [$i];
然后上传
答案
你可以这样试试
$file_names=['IKPI','NPWP']//save what filename would you want to give
for($i = 0; $i < $number_of_files; $i++){
$_FILES['file']['name'] = $file_names[$i];
$_FILES['file']['type'] = $_FILES['multipartFiles']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['multipartFiles']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['multipartFiles']['error'][$i];
$_FILES['file']['size'] = $_FILES['multipartFiles']['size'][$i];
// File upload configuration
$uploadPath = 'uploads/files/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Upload file to server
if($this->upload->do_upload('file')){
// Uploaded file data
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['uploaded_on'] = date("Y-m-d H:i:s");
}
}
以上是关于在codeigniter中上传多个文件,并使用数组重命名每个文件的主要内容,如果未能解决你的问题,请参考以下文章