[英]Codeigniter multiple upload with different area name and rename the file before uploaded
I have a problem with multiple files upload with different area name and want to change for every area filename before it's upload. 我在上传具有不同区域名称的多个文件时遇到问题,并且想要在上传之前更改每个区域文件名。
This is HTML form. 这是HTML表单。
<input type="file" placeholder="" name="profilPic"/>
<input type="file" placeholder="" name="topPic"/>
This is controller 这是控制器
$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.");
}
Note: The pictures are upload to directory. 注意:图片已上传到目录。 But i want to rename every file file name before uploaded like "userID_profil.jpg" and "userID_top.jpg"
但我想在上传之前重命名每个文件的文件名,例如“ userID_profil.jpg”和“ userID_top.jpg”
I solved it. 我解决了
$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", ".");
}
}
You can set the $config['file_name']
before the second file upload using 您可以使用以下方法在第二个文件上传之前设置
$config['file_name']
$this->upload->initialize($config);
Of course, you also need to set it for the first file either with $this->load->library('upload', $config)
or $this->upload->initialize($config)
. 当然,您还需要使用
$this->load->library('upload', $config)
或$this->upload->initialize($config)
为第一个文件设置它。
Docs: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences 文件: https : //www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.