Loading...
Loading...
Loading...
Loading...

How to Upload multiple files using codeigniter

View: 1015    Dowload: 0   Comment: 0   Post by: hanhga   Category: Codeigniter   Fields: Other

CodeIgniter is a PHP framework, CodeIgniter has a number of helpers and libraries .

which will reduce the development time and we can write more reliable and bugs free Code. Here ,This post is about Upload multiple files using codeigniter, CodeIgniter has upload library , by using this Class we can upload files on server very easily.

For Uploading a files, as usually we need a Simple HTML from, with a input field and submit button.

Uploading multiple files using codeigniter is very easy.

First set the file name to userfile[](we need to send the file name in array).We need to send the multiple file name in array.Do not forget to set ‘enctype’=>’multipart/form-data’.

<html>
<body>
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" enctype='multipart/form-data'>
<label for="file">Filename:</label>
<input type="file" name="userfile[]" id="file" multiple>
<input type="submit" value="upload"></form>
</body>
</html>
<?php
  class My_Controller extends CI_Controller {
  public function __construct() {
  parent::__construct();
$this->load->library('upload');
}
 public function file_upload(){
              $files = $_FILES;
                $cpt = count($_FILES['userfile']['name']);
                 for($i=0; $i<$cpt; $i++)
                {
                $_FILES['userfile']['name']= $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];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload();
                $fileName = $_FILES['userfile']['name'];
                 $images[] = $fileName;
}
  $fileName = implode(',',$images);
  $this->my_model->upload_image($fileName);
}
private function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = './upload/'; //give the path to upload the image in folder
         $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '0';
         $config['overwrite'] = FALSE;
  return $config;
  }
}

In Model:

<?php
class My_Model extends CI_Model {
public function upload_image($fileName)
{
if($filename!='' ){
      $filename1 = explode(',',$filename);
  foreach($filename1 as $file){
  $file_data = array(
  'name' => $file,
  'datetime' => date('Y-m-d h:i:s')
  );
  $this->db->insert('uploaded_files', $file_data);
  }
  }
}
}

Hope this article will help you.

How to Upload multiple files using codeigniter

CodeIgniter is a PHP framework, CodeIgniter has a number of helpers and libraries .

Posted on 08-01-2016 

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
File suggestion for you
File suggestion for you
File suggestion for you
Loading...
Loading...
Loading...
Loading...
File top downloads
File top downloads
File top downloads
File top downloads
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd