Codeigniter : Multiple image Upload
example tables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
CREATE TABLE `photos` ( `id` int(11) NOT NULL, `image` varchar(350) NOT NULL, `user_id` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE `user` ( `u_id` int(11) NOT NULL, `name` varchar(350) NOT NULL, `class` varchar(350) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `photos` ADD PRIMARY KEY (`id`); ALTER TABLE `user` ADD PRIMARY KEY (`u_id`); ALTER TABLE `photos` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; ALTER TABLE `user` MODIFY `u_id` int(11) NOT NULL AUTO_INCREMENT; |
autoload customize
1 |
$autoload['libraries'] = array('database', 'email', 'session', 'upload'); |
Controllers Welcome.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { /** * Index Page for this controller. * * Maps to the following URL * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name> * @see https://codeigniter.com/user_guide/general/urls.html */ public function __construct() { parent::__construct(); $this->load->helper('url'); /***** LOADING HELPER TO AVOID PHP ERROR ****/ $this->load->model('Welcome_model','welcome'); /* LOADING MODEL * Welcome_model as welcome */ } public function index() { $this->load->view('welcome_message'); } public function file_upload(){ $files = $_FILES; $count = count($_FILES['userfile']['name']); for($i=0; $i<$count; $i++) { $_FILES['userfile']['name']= time().$files['userfile']['name'][$i]; $_FILES['userfile']['type']= $files['userfile']['type'][$i]; $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_FILES['userfile']['error']= $files['userfile']['error'][$i]; $_FILES['userfile']['size']= $files['userfile']['size'][$i]; $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = '2000000'; $config['remove_spaces'] = true; $config['overwrite'] = false; $config['max_width'] = ''; $config['max_height'] = ''; $this->load->library('upload', $config); $this->upload->initialize($config); $this->upload->do_upload(); $fileName = $_FILES['userfile']['name']; $images[] = $fileName; } $fileName = implode(',',$images); $this->welcome->upload_image($this->input->post(),$fileName); redirect('welcome/view'); } public function view(){ $this->data['view_data']= $this->welcome->view_data(); $this->load->view('view', $this->data, FALSE); } public function edit($edit_id){ $this->data['edit_data']= $this->welcome->edit_data($edit_id); $this->data['edit_data_image']= $this->welcome->edit_data_image($edit_id); $this->load->view('edit', $this->data, FALSE); } public function deleteimage(){ $deleteid = $this->input->post('image_id'); $this->db->delete('photos', array('id' => $deleteid)); $verify = $this->db->affected_rows(); echo $verify; } public function edit_file_upload(){ $files = $_FILES; if(!empty($files['userfile']['name'][0])){ $count = count($_FILES['userfile']['name']); $user_id = $this->input->post('user_id'); for($i=0; $i<$count; $i++) { $_FILES['userfile']['name']= time().$files['userfile']['name'][$i]; $_FILES['userfile']['type']= $files['userfile']['type'][$i]; $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_FILES['userfile']['error']= $files['userfile']['error'][$i]; $_FILES['userfile']['size']= $files['userfile']['size'][$i]; $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = '2000000'; $config['remove_spaces'] = true; $config['overwrite'] = false; $config['max_width'] = ''; $config['max_height'] = ''; $this->load->library('upload', $config); $this->upload->initialize($config); $this->upload->do_upload(); $fileName = $_FILES['userfile']['name']; $images[] = $fileName; } $fileName = implode(',',$images); $this->welcome->edit_upload_image($user_id,$this->input->post(),$fileName); }else { $user_id = $this->input->post('user_id'); $this->welcome->edit_upload_image($user_id,$this->input->post()); } redirect('welcome/view'); } } |
models Welcome_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php class Welcome_model extends CI_Model { public function __construct() { parent::__construct(); } public function upload_image($inputdata,$filename) { $this->db->insert('user', $inputdata); $insert_id = $this->db->insert_id(); if($filename!='' ){ $filename1 = explode(',',$filename); foreach($filename1 as $file){ $file_data = array( 'image' => $file, 'user_id' => $insert_id ); $this->db->insert('photos', $file_data); } } } public function view_data(){ $query=$this->db->query("SELECT ud.* FROM user ud ORDER BY ud.u_id DESC"); return $query->result_array(); } public function edit_data($id){ $query=$this->db->query("SELECT ud.* FROM user ud WHERE ud.u_id = $id"); return $query->result_array(); } public function edit_data_image($id){ $query=$this->db->query("SELECT photo.* FROM user ud RIGHT JOIN photos as photo ON ud.u_id = photo.user_id WHERE ud.u_id = $id"); return $query->result_array(); } public function edit_upload_image($user_id,$inputdata,$filename ='') { $data = array('name' => $inputdata['name'], 'class' => $inputdata['class']); $this->db->where('u_id', $user_id); $this->db->update('user', $data); if($filename!='' ){ $filename1 = explode(',',$filename); foreach($filename1 as $file){ $file_data = array( 'image' => $file, 'user_id' => $user_id ); $this->db->insert('photos', $file_data); } } } } |
views (upload form) welcome_message.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> <style type="text/css"> ::selection { background-color: #E13300; color: white; } ::-moz-selection { background-color: #E13300; color: white; } body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } a { color: #003399; background-color: transparent; font-weight: normal; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } code { font-family: Consolas, Monaco, Courier New, Courier, monospace; font-size: 12px; background-color: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0 14px 0; padding: 12px 10px 12px 10px; } #body { margin: 0 15px 0 15px; } p.footer { text-align: right; font-size: 11px; border-top: 1px solid #D0D0D0; line-height: 32px; padding: 0 10px 0 10px; margin: 20px 0 0 0; } #container { margin: 10px; border: 1px solid #D0D0D0; box-shadow: 0 0 8px #D0D0D0; } </style> </head> <body> <div align="center" class="container"> <form method="POST" action="<?php echo site_url('welcome/file_upload');?>" enctype='multipart/form-data'> <table border="1"> <tr> <td>Name</td> <td><input type="text" name="name" required id="name" placeholder="Name"></td> </tr> <tr> <td>Class</td> <td><input type="text" name="class" required id="class" placeholder="Class"></td> </tr> <tr> <td>Images</td> <td><input type="file" name="userfile[]" required id="image_file" accept=".png,.jpg,.jpeg,.gif" multiple></td> </tr> <tr> <td colspan="2" align="center"><input style="width: 50%;" type="submit" value="Submit"></td> </tr> </table> </form> </div> </body> </html> |
views (List) view.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<div align="center" class="container"> <p align="center"><a href="<?php echo base_url(); ?>">Upload new data</a></p> <table border="1" style="width:100%"> <tr> <td>S.No</td> <td>Name</td> <td>Class</td> <td>Edit</td> </tr> <?php if(isset($view_data) && is_array($view_data) && count($view_data)): $i=1; foreach ($view_data as $key => $data) { ?> <tr> <td><?php echo $i++ ?></td> <td><?php echo $data['name']; ?></td> <td><?php echo $data['class']; ?></td> <td><a href="<?php echo site_url(); ?>/welcome/edit/<?php echo $data['u_id']; ?>">Edit</a></td> </tr> <?php } endif; ?> </table> </div> |
views edit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<div align="center" class="container"> <form method="POST" action="<?php echo site_url('welcome/edit_file_upload');?>" enctype='multipart/form-data'> <table border="1"> <?php if(isset($edit_data) && is_array($edit_data) && count($edit_data)): $i=1; foreach ($edit_data as $key => $data) { ?> <tr> <td>Name</td> <td><input type="text" name="name" value="<?php echo $data['name']; ?>" id="file" placeholder="name"></td> </tr> <tr> <td>Class</td> <td><input type="text" name="class" value="<?php echo $data['class']; ?>" id="file" placeholder="class"></td> <input type="hidden" name="user_id" value="<?php echo $data['u_id']; ?>" id="file" placeholder="class"> </tr> <?php } endif; ?> <?php if(isset($edit_data_image) && is_array($edit_data) && count($edit_data)): $i=1; foreach ($edit_data_image as $key => $data) { ?> <tr class="imagelocation<?php echo $data['id'] ?>"> <td colspan="2" align="center"> <img src="<?php echo base_url(); ?>uploads/<?php echo $data['image']; ?>" style="vertical-align:middle;" width="80" height="80"> <span style="cursor:pointer;" onclick="javascript:deleteimage(<?php echo $data['id'] ?>)">X</span> </td> </tr> <?php }endif; ?> <tr> <td>Images</td> <td><input type="file" name="userfile[]" id="image_file" accept=".png,.jpg,.jpeg,.gif" multiple></td> </tr> <tr> <td colspan="2" align="center"><input style="width: 50%;" type="submit" value="Submit"></td> </tr> </table> </form> </div> <script type="text/javascript"> function deleteimage(image_id) { var answer = confirm ("Are you sure you want to delete from this post?"); if (answer) { $.ajax({ type: "POST", url: "<?php echo site_url('welcome/deleteimage');?>", data: "image_id="+image_id, success: function (response) { if (response == 1) { $(".imagelocation"+image_id).remove(".imagelocation"+image_id); }; } }); } } </script> |
ref : http://www.2my4edge.com/2016/04/multiple-image-upload-with-view-edit.html