0

[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();
}
TuQuoque
  • 5
  • 3
  • 1
    If you are just uploading a single image and saving it twice with different filenames, it might be more useful to upload it once and copy it - This might be helpful - https://stackoverflow.com/questions/11442779/copy-rename-a-file-to-the-same-directory-without-deleting-the-original-file – TimBrownlaw Apr 17 '20 at 17:55
  • 1
    Also your sample code is hardcoding the image types to be .jpeg when you are allowing other types. You want to use the extension of the actual image in naming your files, or convert them if that's a requirement. – TimBrownlaw Apr 17 '20 at 17:57

2 Answers2

0

Loading the upload library twice in a row, even with different configuration arrays, won't do anything. Codeigniter will ignore the second $this->load->library(); statement if the library is already on memory. This is intended to prevent conflicts, race conditions, etc.

You'd need to:-

  1. load the first instance of the library
  2. process the first upload
  3. unload the first instance of the library
  4. load the second instance of the library
  5. process the second upload

Point number 3 could for example be achieved using unset($this->upload); after processing the first upload.

Dum
  • 1,431
  • 2
  • 9
  • 23
Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
0

Remove this line

$this->load->library('upload', $configClone);

Change

if($this->upload->do_upload('file_data')){
   $this->upload->initialize($configClone);
   if($this->upload->do_upload('file_data')){
        echo "Uploaded";
   }else{
        echo $this->upload->display_errors();
   }
 }else{
   echo $this->upload->display_errors();
 }

Explanation: Javier Larroulet's answer

Dum
  • 1,431
  • 2
  • 9
  • 23
  • Thanks for this answer. Although, I already applied Javier's solution. This is also a big help. Thank you. – TuQuoque Apr 18 '20 at 02:57