如何在 codeigniter 中使用多个输入插入多个文件上传加密文件名?

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

如何在 codeigniter 中使用多个输入插入多个文件上传加密文件名?

我的视图中有多个输入文件,如下所示:

<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">

如何在文件上传时加密文件名,文件将根据我输入的文件数量上传。

例如:

如果我输入3个文件图片,文件数据将上传目录中的3个文件,名称将存储到数据库中。

将此代码放入您的控制器:

$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);
       }
   }
}

你的HTML应该是这样的

<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">

将此代码放入您的控制器

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;
}