HanzZame

Multiple Upload Codeigniter 3.0.4

Feb 29th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. Multiple Upload Codeigniter 3.0.4
  2.  
  3. #VIEW----------------Upload_form.php------------------------------>
  4. <div>
  5.     <b>Multiple Upload form</b><br/>
  6.     <?=form_open_multipart('upload/simpan');?>
  7.         <?php for ($i = 1; $i <=3; $i++): ?>
  8.                 <div class="form_element">
  9.                     <label for="photo<?=$i?>">File <?=$i?></label>
  10.                     <?=form_upload(array('name'     => 'userfile'.$i))?>
  11.                 </div>
  12.         <?php endfor ?>
  13.         <input type="submit" value="Upload" name="kirim"/>
  14.     <?=form_close();?>  
  15. </div>
  16.  
  17.  
  18. #CONTOLLERS-----------------Upload.php------------------------>
  19. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  20.  
  21. class Upload extends CI_Controller {
  22.     function __construct()
  23.     {
  24.         parent::__construct();
  25.         $this->load->helper(array('form', 'url','html'));
  26.         $this->load->model('Upload_model');
  27.     }
  28.  
  29.     function index()
  30.     {
  31.         $this->load->view('upload_form');    
  32.     }
  33.  
  34.    public function simpan()
  35.     {
  36.         if($this->input->post('kirim')) {
  37.             $config['upload_path'] = 'uploads/';
  38.             $config['allowed_types'] = 'gif|jpg|png';
  39.             $config['max_size'] = '3000';
  40.  
  41.             $this->load->library('upload', $config);
  42.  
  43.             $result_array = array();
  44.            
  45.             for ($i = 1; $i <=3; $i++){
  46.                 if (!$this->upload->do_upload('userfile'.$i)){
  47.                         $sub_data['error'] = $this->upload->display_errors();
  48.                  }else{
  49.                   array_push($result_array,$this->upload->data());
  50.                 }
  51.             }
  52.              $sub_data  = array(
  53.              'file' => $result_array[0]['file_name'],
  54.              'file2' => $result_array[1]['file_name'],
  55.              'file3' => $result_array[2]['file_name'],
  56.              );
  57.          }
  58.  
  59.        $this->Upload_model->create($sub_data);
  60.         redirect(site_url('upload'));
  61.     }
  62. }
  63.  
  64.  
  65. #MODEL-------------------Upload_model.php------------------------->
  66. <?php
  67. defined('BASEPATH') OR exit('No direct script access allowed');
  68.  
  69. class Upload_model extends CI_Model {
  70.  
  71.  
  72. public function create($sub_data)
  73.    {
  74.      $this->db->insert('images', $sub_data);  
  75.    }
  76. }
  77.  
  78.  
  79. #DATABASE----------------------------------------------------->
  80. CREATE TABLE IF NOT EXISTS `images` (
  81.   `id` int(10) NOT NULL,
  82.   `file` text NOT NULL,
  83.   `file2` text NOT NULL,
  84.   `file3` text NOT NULL
  85. ) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=latin1;
Advertisement
Add Comment
Please, Sign In to add comment