In this tutorial, I am going to show how to upload multiple files into a folder and insert its name in a database table. In an application, we may either have to upload a single or multiple files. Take freelancer.com portfolio page for instance. They allow freelancers to upload multiple files for the same portfolio item. So our goal here is the same. The goals are:
- Take one or multiple files as an input.
- Once submit is pressed, the file is uploaded.
- When the file is uploaded, an its name is inserted in the database table.
In order to understand this tutorial, you must know how to install, and use codeigniter. Follow the following links if you want to learn these steps.
Now lets begin!
In order to upload a file to a folder, first a view is built that takes File as an input and has a submit button. I am using a controller named c_upload and my view is v_uploader
VIEW: v_uploader
I am taking multiple files as an input.
<?php //echo base_url('index.php/c_uploader/do_upload'); ?> Select One or multiple FilesAllowed files: gif, png, png, pdf
If you wish to upload a single file, then all you need to do is to remove the “multiple” keyword in line 5. Here is the code.
<?php //echo base_url('index.php/c_uploader/do_upload'); ?> Select One or multiple FilesAllowed files: gif, png, png, pdf
CONTROLLER: c_upload
From view, I am calling a function called do_upload(). I also have another function set_upload_options().
Load these helpers
$this->load->helper("URL", "DATE", "URI", "FORM");
Load these libraries
$this->load->library('form_validation'); $this->load->library('upload');
Load this model
$this->load->model('m_upload');
Finally, here are the functions
Note: In set_upload_options() function, you can configure upload path
function do_upload(){ $this->load->library('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(); $upload_data = $this->upload->data(); $file_name = $upload_data['file_name']; $file_type = $upload_data['file_type']; $file_size = $upload_data['file_size']; // Output control $data['getfiledata_file_name'] = $file_name; $data['getfiledata_file_type'] = $file_type; $data['getfiledata_file_size'] = $file_size; // Insert Data for current file $this->m_upload->insertNotices($form_input_Data); //Create a view containing just the text "Uploaded successfully" $this->load->view('upload_success', $data); } } private function set_upload_options(){ // upload an image options $config = array(); $config['upload_path'] = './fileselif/'; $config['allowed_types'] = 'gif|jpg|png|pdf'; $config['max_size'] = '0'; $config['overwrite'] = FALSE; return $config; }
Now the Model code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class M_upload extends CI_Model { function __construct() { parent::__construct(); } function M_notice (){ parent::Model(); } function insertNotices($arrayOfNoticeFiles){ $tableName = "t_notices"; $inputArray = $arrayOfNoticeFiles; $data = array( 'document_foldername' => $inputArray["document_foldername"], 'document_filename' => $inputArray["document_filename"] ); $this->db->insert($tableName, $data); } } /* End of file welcome.php */ /* Location: ./application/controllers/welcome.php */
These are the steps explaining the program.
8 responses to “Codeigniter 2.20 – Uploading files to a folder and Inserting in Database table”
Sir I want to add only one image at a time without any validation . please help me
There are parameters you can set in the config or the controllers. Multiple files and single files can be set just like the file format.
Hello sir, I am getting error like
You did n’t select any file to upload
Did you select a file 😀 ?
Thanks, you are a life saver!
You’re welcome! And thanks for the kind words and visiting my blog 🙂
Let me know in future if you wish to have me write a tutorial that you will like.
that’s useless…. You upload to a directory that is accessible from http://www... nice try but it is dangerous.
Add an index.php with “Directory access is not allowed!” in it and it will fix many problems.
Also, you can put a .htaccess file for denying access to your folder 🙂