1 private function upload_files($path, $title, $files)
2 {
3 $config = array(
4 'upload_path' => $path,
5 'allowed_types' => 'jpg|gif|png',
6 'overwrite' => 1,
7 );
8
9 $this->load->library('upload', $config);
10
11 $images = array();
12
13 foreach ($files['name'] as $key => $image) {
14 $_FILES['images[]']['name']= $files['name'][$key];
15 $_FILES['images[]']['type']= $files['type'][$key];
16 $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
17 $_FILES['images[]']['error']= $files['error'][$key];
18 $_FILES['images[]']['size']= $files['size'][$key];
19
20 $fileName = $title .'_'. $image;
21
22 $images[] = $fileName;
23
24 $config['file_name'] = $fileName;
25
26 $this->upload->initialize($config);
27
28 if ($this->upload->do_upload('images[]')) {
29 $this->upload->data();
30 } else {
31 return false;
32 }
33 }
34
35 return $images;
36 }
37
1views
2---------
3
4<?php echo validation_errors();?>
5<?php echo form_open_multipart('pages/multiple_files');?>
6<p><input type="file" multiple="multiple" name="image_name[]" class="form-control" /></p>
7
8<input type="submit" class="btn btn-success btn-block"/>
9</form>
10
11Controller
12----------
13
14public function multiple_files(){
15 $this->load->library('upload');
16 $image = array();
17 $ImageCount = count($_FILES['image_name']['name']);
18 for($i = 0; $i < $ImageCount; $i++){
19 $_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
20 $_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
21 $_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
22 $_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
23 $_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
24
25 // File upload configuration
26 $uploadPath = './assets/images/profiles/';
27 $config['upload_path'] = $uploadPath;
28 $config['allowed_types'] = 'jpg|jpeg|png|gif';
29
30 // Load and initialize upload library
31 $this->load->library('upload', $config);
32 $this->upload->initialize($config);
33
34 // Upload file to server
35 if($this->upload->do_upload('file')){
36 // Uploaded file data
37 $imageData = $this->upload->data();
38 $uploadImgData[$i]['image_name'] = $imageData['file_name'];
39
40 }
41 }
42 if(!empty($uploadImgData)){
43 // Insert files data into the database
44 $this->pages_model->multiple_images($uploadImgData);
45 }
46 }
47
48Model
49---------
50 public function multiple_images($image = array()){
51
52 return $this->db->insert_batch('profile_images',$image);
53 }
54
55