Upload multiple files in multiple folders using Codeigniter:
CODEIGNITER: upload multiple files into database with multiple folder which destination path you set for upload file. Follow the tutorial.
Here I will help you to insert multiple image file in a single row of MySQL table and file upload in multiple folder in your web page. In codeigniter need to set form_open_multipart() for file upload in your website.
You need to create a form with multiple input file option and create multiple folder in your project. That’s like:
File Upload Form:
<div class="row"> <?php $att = array('method'=>"post"); echo form_open_multipart('MyController/file_upload',$att)?> <input type="file" name="banner" accept="image/png, image/jpeg, image/gif"><br/> <input type="file" name="word_image" accept="image/png, image/jpeg, image/gif"><br/> <input type="file" name="pdf_image" accept="image/png, image/jpeg, image/gif"><br/> <input type="submit" name="submit"> <?php echo form_close();?> </div>
Controller:
Access model class and define a function name and go to the model page.
<?php class MyController extends CI_controller{ public function __construct(){ parent::__construct(); $this->load->helper('url'); $this->load->helper('form'); $this->load->helper(array('form', 'url')); $this->load->model('MyModel'); } public function index(){ $this->load->view('file_upload'); } public function file_upload(){ $all=array(); $all['data']=$this->MyModel->insert_file(); } } ?>
Model:
Get file value and insert image value into database with multiple folders. Follow example:
<?php class MyModel extends CI_Model { public function __construct(){ parent::__construct(); $this->load->database(); } public function insert_file(){ $config['upload_path'] = './assets/file_banner'; $config['allowed_types'] = '*'; $this->load->library('upload', $config); $this->upload->do_upload('banner'); $file_banner = $this->upload->data(); unset($config); $word1 = $_FILES['word_image']['name']; $config['upload_path'] = './assets/file_word/'; $config['allowed_types'] = 'gif|jpg|jpeg|png'; $config['max_size'] = 2048; $config['max_width'] = 2024; $config['max_height'] = 2024; $this->upload->initialize($config); $this->upload->do_upload('word_image'); unset($config); $pdf_image = $_FILES['pdf_image']['name']; $config['upload_path'] = './assets/file_pdf/'; $config['allowed_types'] = 'gif|jpg|jpeg|png'; $config['max_size'] = 2048; $config['max_width'] = 2024; $config['max_height'] = 2024; $this->upload->initialize($config); $this->upload->do_upload('pdf_image'); $data=array('file_banner'=>$file_banner['file_name'],'file_word'=>$word1,'file_pdf'=>$pdf_image); $this->db->insert('file_table',$data); } } ?>
Related posts