[SOLVED]
I am having a problem in function in my controller. I want to upload a single image twice and save it in the same directory but have different file names.
Here's my form(view.php):
<form action="<?php echo base_url("process/testFunction"); ?>" enctype="multipart/form-data" method="post">
<input type="file" name="file_data" size="50" required>
<input type="hidden" name="user_id" value="<?php echo $this->session->userdata('user_id'); ?>" readonly>
<input type="hidden" name="purpose" value="Picture" readonly>
<input type="submit" value="Save Changes">
</form>
controller.php:
function testFunction(){
$userID = $this->input->post('user_id');
$purpose = $this->input->post('purpose');
$randomText = time();
if($purpose == "Picture"){
$uploadPath = "./uploads/images/";
$allowedTypes = "jpg|jpeg|png";
$maxSize = 10000000;
$fileName = "PP_".$userID.".jpeg";
$fileName2 = "PP_".$userID."_".$randomText.".jpeg"; //for backup
}
$config = array(
'upload_path' => $uploadPath,
'allowed_types' => $allowedTypes,
'max_size' => $maxSize,
'file_name' => $fileName
);
$configClone = array(
'upload_path' => $uploadPath,
'allowed_types' => $allowedTypes,
'max_size' => $maxSize,
'file_name' => $fileName2
);
$this->load->library('upload', $config);
$this->load->library('upload', $configClone);
if($this->upload->do_upload('file_data')){
echo "Uploaded";
}else{
echo $this->upload->display_errors();
}
}
What is currently happening in my code is. Although it uploads 2 same images. The file name with "Random Text" is not working.
[Solution]: controller file. I modified some lines.
Reference: Javier's answer below
$this->load->library('upload', $config);
if($this->upload->do_upload('file_data')){
unset($this->upload);
$this->load->library('upload', $configClone);
$this->upload->do_upload('file_data');
echo "Uploaded";
}else{
echo $this->upload->display_errors();
}