Codeigniter - SPLessons

CodeIgniter File Upload

Home > Lesson > Chapter 22
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter File Upload

CodeIgniter File Upload

shape Description

CodeIgniter has extensive helpers and libraries with classes that makes the code reliable.One of that class is "Upload Class" which shows an easy way to upload files on Server and validate by restricting the size and type of the file that has to be uploaded.

shape Example

Step-1 : Initially create a destination folder uploads to save all the uploaded files. Step-2 : Create the controller uploadfile.php inside the folder application\controllers in CodeIgniter and enter the below code. [php] <?php class uploadfile extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } //index function function index() { //load file upload form $this->load->view('upload_file_view'); } //file upload function function upload() { //set preferences $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'txt|pdf'; $config['max_size'] = '100'; //load upload class library $this->load->library('upload', $config); if (!$this->upload->do_upload('filename')) { // case - failure $upload_error = array('error' => $this->upload->display_errors()); $this->load->view('upload_file_view', $upload_error); } else { // case - success $upload_data = $this->upload->data(); $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>'; $this->load->view('upload_file_view', $data); } } } ?> [/php] In the above controller there is a method upload() where the actual code for the file uploading takes place. The library file upload is loaded initially using $this->load->library('upload', $config); statement in which array $config possess the preferences(like file size, path,extensions) that controls the uploading process. When the file is submitted, upload() method of controller invokes and validation of file takes place.If any error occurs during validation, file uploading stops and if no error, file will be uploaded successfully. Below are few methods of 'upload' class library in the controller:
  • $this->upload->do_upload() : This method uploads the selected file to the destination folder.Providing parameters to this method is optional and if not declared, then it expects the file from the input field with name userfile. If other name is given to 'userfile', then pass the field name as parameter to the do_upload() function.
  • $this->upload->display_errors() : Error message is returned by this method if the do_upload() method returns false.
  • $this->upload->data() : An array of data is returned related to file uploaded like the file name, path, size etc.
Step-3 : Create the view file upload_file_view.php inside application\views folder. This view file contains a simple upload form with file input and a submit button. [html] <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CodeIgniter File Upload Form</title> <!-- load bootstrap css file --> <link href="<?php echo base_url("assets/bootstrap/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3 well"> <legend>CodeIgniter File Upload Demo</legend> <?php echo form_open_multipart('uploadfile/upload');?> <fieldset> <div class="form-group"> <div class="row"> <div class="col-md-12"> <label for="filename" class="control-label">Select File to Upload</label> </div> </div> </div> <div class="form-group"> <div class="row"> <div class="col-md-12"> <input type="file" name="filename" size="20" /> <span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span> </div> </div> </div> &nbsp; <div class="form-group"> <div class="row"> <div class="col-md-12"> <input type="submit" value="Upload File" class="btn btn-primary"/> </div> </div> </div> </fieldset> <?php echo form_close(); ?> <?php if (isset($success_msg)) { echo $success_msg; } ?> </div> </div> </div> </body> </html> [/html] Output :

Uploading Images

shape Description

To upload only the images in Codeigniter, then set the preferences to something like this in the Controller file [php] <?php $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'png|jpg|gif'; $config['max_size'] = '150'; $config['max_width'] = '1024'; /* max width of the image file */ $config['max_height'] = '768'; /* max height of the image file */ ?> [/php] Moreover all the image files should fall under the maximum file size, width and height set, that explains the file and image uploading process in CodeIgniter.

Summary

shape Key Points

  • $this->load->library('upload', $config); is loaded which sets and uploads the files with the preferences.