CodeIgniter – Image and File Upload
This post is for file upload in CodeIgniter(C.I.). In CodeIgniter(C.I.) By the use file uploading class, we can easily upload a file or an image or any documents.
In CodeIgniter(C.I.) we can assign it size, types, width, height.
By using this code and instruction we can CodeIgniter in CodeIgniter(C.I.).
First we have to Create a destination folder(where we have to store file or image or other document).
So, Create a folder and give named “uploads_file”(what is in your mind) at the root of your CodeIgniter(CI).
codeigniter_projct/ .htaccess index.php application/ assets/ system/ uploads_file/ <--- create/add folder hear(on root)
Now we need to create 2 view file(file_upload_view.php and upload_file_success.php) and controller(upload_file_controller.php)
View : file_upload_view.php
this file is use for upload file and pass to controller.
View : upload_file_success.php
It will display result of success or error.
View : upload_file_controller.php
It will work for upload file and return to result to view file. where we load a library to initialize an Upload class using the “upload” library :-
$this->load->library('upload');
we can also set preferences for image or file or document i.e. size, type, etc. in config.php file.
// add your upload path $config['upload_path'] = './uploads_file/'; // allow types of file $config['allowed_types'] = 'gif|jpg|png'; // allow maxim size of file $config['max_size'] = '125'; // allow maxim width of file $config['max_width'] = '2048'; // allow maxim height of file $config['max_height'] = '1024';
View : file_upload_view.php
<html> <head> <title>Upload Form</title> </head> <body> <?php $error; //Display error if exist // assign form tag echo form_open_multipart('upload_file_controller/do_file_upload'); echo "<input type='file' name='user_file' size='35' />"; echo "<input type='submit' name='submit_file' value='upload' /> "; echo "</form> "; // end form tag ?> </body> </html>
Controller file : upload_file_controller.php
// check BASEPATH is defined or exit. if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Upload_File_Controller extends CI_Controller { public function __construct() { // call function on page lode, if any need parent::__construct(); } public function file_upload_view(){ $this->load->view('file_upload_view', array('error' => ' ' )); } public function do_file_upload(){ $file_config = array( 'upload_path' => "./uploads_file/", // add your uploads file location 'allowed_types' => "gif|jpg|png|jpeg|pdf", // add your type of file 'overwrite' => TRUE, // overwrite is allow hear 'max_size' => "2048000", // add your max file size for upload , here we use 2 MB(2048 Kb) 'max_height' => "1024", // add your height 'max_width' => "2048" // add your width ); // include upload library for file upload $this->load->library('upload', $file_config); if($this->upload->do_file_upload()) { $file_upload_data = array('upload_data' => $this->upload->data()); $this->load->view('upload_file_success',$file_upload_data); } else { $file_upload_error = array( 'error' => $this->upload->display_errors() ); $this->load->view('file_upload_view', $file_upload_error); } } }
View : upload_file_success.php
<html> <head> <title>Upload File Success</title> </head> <body> <h3>file has been uploaded successfully!</h3> <!-- Success message show hear --> <ul> <?php // print value form controller foreach ($upload_data as $item => $value): // start foreach loop ?> <li><?php echo $item;?>: <?php echo $value;?></li> <!-- display result --> <?php endforeach; // end foreach loop ?> </ul> <?php // show file_upload_view.php page echo anchor('upload_file_controller/file_upload_view', 'Upload Another File Hear!'); ?> </body> </html>