我在上传多个文件时遇到了问题,这些文件上传了不同的区域名称,并希望在上传之前更改每个区域文件名。
这是HTML表单。
<input type="file" placeholder="" name="profilPic"/>
<input type="file" placeholder="" name="topPic"/>
这是控制器
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
//$config['file_name'] = $this->session->sersession["id"];
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "profil pic was not uploaded= ");
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "profil picture was uploaded.");
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "top pic was not uploaded" );
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "this picture was uploaded.");
}
注意:图片会上传到目录。但我想在上传之前重命名每个文件名,例如&#34; userID_profil.jpg&#34;和&#34; userID_top.jpg&#34;
答案 0 :(得分:1)
我解决了。
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
if($_FILES["profilPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_profil.jpg";
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", ".");
}else{
$profilPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}
if($_FILES["topPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_top.jpg";
if($_FILES["profilPic"]["name"]){
$this->upload->initialize($config);
}else{
$this->loadl->library('upload', $config);
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "" );
}else{
$topPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}
答案 1 :(得分:0)
您可以使用
在第二次文件上传之前设置$config['file_name']
$this->upload->initialize($config);
当然,您还需要使用$this->load->library('upload', $config)
或$this->upload->initialize($config)
为第一个文件设置它。
文档:https://www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences