0

How to insert multiple file upload encrypted filename with multiple input in codeigniter ?

I have multiple input file in my view like this:

<input name="picture_1" class="form-control" style="padding-top: 0;" type="file">
<input name="picture_2" class="form-control" style="padding-top: 0;" type="file">
<input name="picture_3" class="form-control" style="padding-top: 0;" type="file">
<input name="picture_4" class="form-control" style="padding-top: 0;" type="file">
<input name="picture_5" class="form-control" style="padding-top: 0;" type="file">

How to make filename encrypted when file uploaded and file will upload based on how many I input file.

for example:

if I input 3 file picture, file data will upload 3 file in directory, and name will stored to database.

silvia zulinka
  • 728
  • 3
  • 18
  • 39
  • Possible duplicate of [Codeigniter Multiple File Upload Encryption Issue](http://stackoverflow.com/questions/16376805/codeigniter-multiple-file-upload-encryption-issue) –  Nov 24 '16 at 01:12

2 Answers2

0

place this code to your controller:

$config['upload_path']      = './assets/img/packet/';
$config['allowed_types']    = 'jpg|png|gif|jpeg';
$config['max_size']         = 125000; // 1 GB
$config['encrypt_name']     = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);

for ($img=0;$img<=5;$img++)
{
   if (!empty($_FILES['picture_'.$img]))
   {
       if ($this->upload->do_upload('picture_'.$img))
       {
           $uploaded = $this->upload->data();
           $data_picture = array('filename'  => $uploaded['file_name']);
           $this->db->insert('db_picture', $data_picture);
       }
   }
}
silvia zulinka
  • 728
  • 3
  • 18
  • 39
0

Your HTML should be like this

<input name="item_image[]" class="form-control" style="padding-top: 0;" type="file">
<input name="item_image[]" class="form-control" style="padding-top: 0;" type="file">
<input name="item_image[]" class="form-control" style="padding-top: 0;" type="file">
<input name="item_image[]" class="form-control" style="padding-top: 0;" type="file">
<input name="item_image[]" class="form-control" style="padding-top: 0;" type="file">

Place this code into your controller

public function save_multiple_images()
{
  $this->load->library('upload');
  for($k=0; $k<$total_items; $k++;)
  {

      if(isset($_FILES['item_image']['name'][$k]))
      {

        $files = $_FILES;
        $_FILES['userfile']['name'] = $files['item_image']['name'][$k];
        $_FILES['userfile']['type']= $files['item_image']['type'][$k];
        $_FILES['userfile']['tmp_name']= $files['item_image']['tmp_name'][$k];            
        $_FILES['userfile']['error']= $files['item_image']['error'][$k];
        $_FILES['userfile']['size']= $files['item_image']['size'][$k];
        $fileName = $_FILES['userfile']['name'];
        //Calls set upload funtions
        $this->upload->initialize($this->set_upload_options($k));
        if($fileName!=''){
          $this->upload->do_upload();
          $upload_data = $this->upload->data();
          $fileSize = $upload_data['file_size'];
          if($this->upload->display_errors()){
            echo json_encode($this->upload->display_errors()); exit;
          }
          $fileName = base_url('files/reward_images/'.$upload_data['file_name']);
        }
        else{
          $fileName = base_url('files/reward_images/no_image.png');
        }
        $reward_image = $fileName;
        $ktem_id =0;
        $res = $this->rewards_model->save_reward_item($reward_image);
      }
   }
}

private function set_upload_options($k)
{
  //upload an image options
  $config = array();
  $config['upload_path'] = './files/reward_images/';
  $config['file_name'] = 'File'.time().$k;
  $config['allowed_types'] = '*'; \
  $config['max_size'] = '52528800';
  $config['overwrite'] = FALSE;
  return $config;
}
Eswaran Arumugam
  • 77
  • 1
  • 2
  • 11
  • I have try your suggestion and find this way, its working.. http://stackoverflow.com/questions/11524356/multiple-files-upload-array-with-codeigniter-2-0 – silvia zulinka Oct 27 '16 at 06:50