Making File Upload System Using CodeIgniter Framework

In This Tutorial I will discuss How to Make File Upload System Using PHP CodeIgniter Framework.

At Essentially in CodeIgniter to do upload file we have given her class to help us developers, and this time we will discuss the class.

Let’s discussed step by step below:

  1. Create upload_form.php file and place it in the application / views and type the following script:
 <html>

<head>

<title>CodeIgniter Upload Form </title>

</head>

<body>

<?php echo $error;?>

<?php echo form_open_multipart('fileupload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>

</html>
 

2.       Then create upload_success.php file and place it in application / views and contents of the script as follows:

 <html>

<head>

<title>File Upload Success </title>

</head>

<body>

<h3>Your file was uploaded successfully </h3>

<ul>

<?php foreach ($upload_data as $item => $value):?>

<li><?php echo $item;?>: <?php echo $value;?></li>

<?php endforeach; ?>

</ul>

<p>

<?php echo anchor('upload', 'Other files uploaded!'); ?>

</p>

</body>

</html>
 

3.       If you’ve made the files for views, now we create a file for the controllers. Create fileupload.php and place in application / controllers / with the following script:

 <?php

class Upload extends CI_Controller {

function __construct()

{

parent::__construct();

$this->load->helper(array('form', 'url'));

}

function index()

{

$this->load->view('upload_form', array('error' => ' ' ));

}

function do_upload()

{

$config['upload_path'] = './uploads/';

$config['allowed_types'] = 'gif|jpg|png';

$config['max_size']    = '100000';

$config['max_width']  = '1024';

$config['max_height']  = '768';

$this->load->library('upload', $config);

$this->upload->initialize($config);

if ( ! $this->upload->do_upload())

{

$error = array('error' => $this->upload->display_errors());

$this->load->view('upload_form', $error);

}

else

{

$data = array('upload_data' => $this->upload->data());

$this->load->view('upload_success', $data);

}

}

}

?>
 

Here I Give a little explanation upload.php:

$this->load->helper(array(‘form’, ‘url’)); Meaning that it can be removed if you have defined in config / autoload.
$config[‘upload_path’] = ‘./uploads/’; Location or folder you want to be created first file uploads folder on your CI web folder. (Create a new folder file storage)
$config[‘allowed_types’] = ‘gif|jpg|png’; file types are allowed to be uploaded, it can be tailored to the needs.
$config[‘max_size’] = ‘100’;$config[‘max_width’] = ‘1024’;$config[‘max_height’] = ‘768’; file size and length to the width of the image, it can also be customized according to the needs.

If all the above stages have been completed correctly you created, now you just try to go to the link http://localhost/Your_CodeIgniter _Folder/ index.php/fileupload/

Now see the Final Result screenshots below:

File Upload Form

Figure 1: File Upload Form

CodeIgniter File Upload Success

Figure 2: File Upload Success Page

Download

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I graduated in 2009 with a bachelor’s degree in Engineering from State University Of Bangladesh, I’m also a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, CIDA and MAX Group where I worked on ERP software and web development., but now i’m a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.
4 comments on “Making File Upload System Using CodeIgniter Framework

Leave a Reply

Your email address will not be published. Required fields are marked *