将图片上传到两个不同的路径 [英] Upload an image to two different paths

查看:120
本文介绍了将图片上传到两个不同的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to upload an image and save it in two different folders, ./imgup/web/data_dinamis/ and ./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/'

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

推荐答案

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

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

注意: $ this->之后; load-> library('upload',$ config); 使用 $ this-> upload-> initialize($ config);
如果您自动加载类,请参见docs 此处

这篇关于将图片上传到两个不同的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆