上传一张图片到两个不同的路径

Upload an image to two different paths

如何上传图片并将其保存在两个不同的文件夹中,./imgup/web/data_dinamis/./imgup/web/video/

我上传图片的代码:

$config['upload_path'] = './imgup/web/data_dinamis/'; 
$config['upload_path'] = './imgup/web/video/'; 
$config['allowed_types'] = '*'; 
$config['file_name'] = $nama_baru;

$this->load->library('upload',$config);
$this->upload->do_upload('file');
$this->upload->display_errors();
$berita = array(
 'user_id' =>  $this->session->userdata('user')->user_id,
 'kategori_id' => $this->input->post('kategori_id'),
 'judul' => $this->input->post('judul'),
 'gambar' =>  $nama_baru,
);

所有图片只保存在'./imgup/web/data_dinamis/',不保存在'./imgup/web/video/'

最简单的方法是对您可能要上传到的每个路径使用 ->do_upload()

$p1='./imgup/web/data_dinamis/';
$p2='./imgup/web/video/';

//create an array of pathes:
$p=array($p1, $p2);

// get length of array
$c=count($p);

// loop through
for ($i=0;$i<$c;$i++){    
  $config['upload_path'] =$p[$i];
  $config['allowed_types'] = '*'; 
  $config['file_name'] = $nama_baru;

  $this->load->library('upload',$config);
  $this->upload->initialize($config);
  $this->upload->do_upload('file');
  $this->upload->display_errors();
}

$berita = array(
   'user_id' =>  $this->session->userdata('user')->user_id,
   'kategori_id' => $this->input->post('kategori_id'),
   'judul' => $this->input->post('judul'),
   'gambar' =>  $nama_baru,
);

注意:$this->load->library('upload',$config); 之后使用 $this->upload->initialize($config); 如果您自动加载 class,这很有用,请参阅文档 here