Upload an image to two different paths

送分小仙女□ 提交于 2019-11-28 10:42:14

问题


How to upload an image and save it in two different folders, ./imgup/web/data_dinamis/ and ./imgup/web/video/ ?

My code to upload image :

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

All images are saved only in './imgup/web/data_dinamis/', but not in './imgup/web/video/'.


回答1:


the easiest would be to use ->do_upload() for each of the path you might want to upload to:

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

note: after $this->load->library('upload',$config); use $this->upload->initialize($config); This is useful if you auto-load the class, see docs here



来源:https://stackoverflow.com/questions/57588171/upload-an-image-to-two-different-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!