Advertisement
azmicolejr

Upload Banyak Gambar, Tampilkan Preview dan Simpan ke Databa

Aug 13th, 2017
2,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. ----------------------------------------------------------------------------
  2. CONTROLLER
  3. ----------------------------------------------------------------------------
  4. public function tambah_proses()
  5. {
  6.     $this->load->library('upload');
  7.     $dataInfo = array();
  8.     $files = $_FILES;
  9.     $cpt = count($_FILES['userfile']['name']);
  10.     for($i=0; $i<$cpt; $i++)
  11.     {
  12.         $_FILES['userfile']['name']= $files['userfile']['name'][$i];
  13.         $_FILES['userfile']['type']= $files['userfile']['type'][$i];
  14.         $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
  15.         $_FILES['userfile']['error']= $files['userfile']['error'][$i];
  16.         $_FILES['userfile']['size']= $files['userfile']['size'][$i];
  17.  
  18.         $this->upload->initialize($this->set_upload_options());
  19.         $this->upload->do_upload();
  20.         $dataInfo[] = $this->upload->data();
  21.     }
  22.  
  23.     $data = array(
  24.         'judul_produk' => $this->input->post('judul_produk'),
  25.         'userfile' => $dataInfo[0]['file_name'],
  26.         'userfile2' => $dataInfo[1]['file_name'],
  27.         'userfile3' => $dataInfo[2]['file_name'],
  28.      );
  29.  
  30.      $this->Produk_model->insert($data);
  31. }
  32.  
  33. private function set_upload_options()
  34. {
  35.     $config = array();
  36.     $config['upload_path'] = 'images/';
  37.     $config['allowed_types']    = 'jpg|jpeg|png|gif';
  38.     $config['max_size']         = '2048'; // 2 MB
  39.  
  40.     return $config;
  41. }
  42.  
  43. ----------------------------------------------------------------------------
  44. MODEL
  45. ----------------------------------------------------------------------------
  46. function insert($data)
  47. {
  48.   $this->db->insert('namatabel', $data);
  49. }
  50.  
  51. ----------------------------------------------------------------------------
  52. VIEW
  53. ----------------------------------------------------------------------------
  54. <?php echo form_open_multipart(site_url('tambah_proses'));?>
  55. <div class="form-group"><label>Gambar</label>
  56.   <input type="file" name="userfile[]" multiple>
  57. </div>
  58. <?php echo form_close() ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement