在 Codeigniter 中上传多个文件并更改名称文件
Posted
技术标签:
【中文标题】在 Codeigniter 中上传多个文件并更改名称文件【英文标题】:Multiple files upload in Codeigniter and Changes the name files 【发布时间】:2020-03-31 01:39:06 【问题描述】:我的控制器:
$foto = $_FILES['foto']['name'];
$doc = $_FILES['doc']['name'];
$ambextFoto = explode(".",$foto);
$ambextDOc = explode(".",$doc);
$ekstensiFoto = end($ambextFoto);
$ekstensiDoc = end($ambextDoc);
$nama_baru_foto = 'foto-'.date('YmdHis');
$nama_baru_doc = 'doc-'.date('YmdHis');
$nama_file_foto = $nama_baru_foto.".".$ekstensiFoto;
$nama_file_doc = $nama_baru_doc.".".$ekstensiDoc;
$config['upload_path'] = './assets/userfiles/usulan/';
$config['allowed_types'] = 'pdf';
$config['file_name'] = $nama_file_foto;
$config['file_name'] = $nama_file_doc;
$this->upload->initialize($config);
if($this->upload->do_upload('foto') AND $this->upload->do_upload('doc'))
$data = array(
'member_id' => $this->session->userdata('member_id'),
'foto' => $foto,
'doc' => $doc
);
else
$this->session->set_flashdata('error','Upload File Failed');
redirect(site_url('data/usulan/add'));
我的观点:
<form action="<?php echo site_url('data/usulan/insert') ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" class="form-control" name="foto" accept="application/pdf">
</div>
<div class="form-group">
<input type="file" class="form-control" name="doc" accept="application/pdf">
</div>
<div class="card-footer text-right">
<button type="submit" class="btn btn-primary ml-auto">Submit</button>
</div>
</form>
文件已上传,但名称文件仅在变量 $name_file_doc 中上传。所有文件均已上传,但只有名称为 $name_file_doc。 请帮帮我
【问题讨论】:
参考***.com/a/40778817/6309457 这能回答你的问题吗? Multiple image upload with CodeIgniter 所有文件都已上传,但所有文件名都是 doc-date('YmdHis') 而不是 foto = foto-date('YmdHis') 和 doc = doc-date('YmdHis') 【参考方案1】:使用这个:
if(isset($_FILES['foto']['name']))
//write only foto file upload code here.
if(isset($_FILES['doc']['name']))
//write only doc file upload code here.
【讨论】:
以上是关于在 Codeigniter 中上传多个文件并更改名称文件的主要内容,如果未能解决你的问题,请参考以下文章