Single File Upload in CodeIgniter Framework


Create new folder named assets in root project. In this folder, create new folders as below:

Create new folder named images in assets folder.

In CodeIgniter Project, Open autoload.php file in config folder. Add form and url helper to helper config as below:

$autoload['helper'] = array('form', 'url');

In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:

$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';




Create new PHP file named demo.php in controllers folder as below:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Demo extends CI_Controller
{

    public function index()
    {
        $this->load->view('demo/index');
    }

    function do_upload()
    {
        $config['upload_path'] = './assets/images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $config['encrypt_name'] = TRUE;
        $config['remove_spaces'] = TRUE;

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

        if (! $this->upload->do_upload('photo')) {
            $error = array(
                'error' => $this->upload->display_errors()
            );
            $this->load->view('demo/index', $error);
        } else {
            $data['fileInfo'] = array(
                'upload_data' => $this->upload->data()
            );
            $this->load->view('demo/success', $data);
        }
    }
}

In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:

$route['default_controller'] = 'demo';




Create new folder named demo in views folder. In this folder, create new views as below:

Create new PHP file named index.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
	<head>
		<title>Demo Page</title>
	</head>
	<body>

		<h3>Upload File</h3>

		<?php echo isset($error) ? $error : ''; ?>

		<?php echo form_open_multipart('demo/do_upload'); ?>
        	<input type="file" name="photo" />
            <br /><br />
            <input type="submit" value="Upload" />
		<?php echo form_close(); ?>

	</body>
</html>

Create new PHP file named success.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
    <head>
        <title>Success Page</title>
    </head>
    <body>

    	<h3>File Info</h3>
    	<table cellpadding="2" cellspacing="2" border="1">
    		<tr>
    			<td>File Name</td>
    			<td><?php echo $fileInfo['upload_data']['file_name']; ?></td>
    		</tr>
    		<tr>
    			<td>File Type</td>
    			<td><?php echo $fileInfo['upload_data']['file_type']; ?></td>
    		</tr>
    		<tr>
    			<td>File Size</td>
    			<td><?php echo $fileInfo['upload_data']['file_size']; ?> byte(s)</td>
    		</tr>
    		<tr>
    			<td>Is Image</td>
    			<td><?php echo $fileInfo['upload_data']['is_image']; ?></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td>
    				<img src="<?php echo base_url(); ?>/assets/images/<?php echo $fileInfo['upload_data']['file_name']; ?>" width="120">
    			</td>
    		</tr>
    	</table>

    </body>
</html>




Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index

Output

Select a image and upload file

Output

I recommend you refer to the books below to learn more about the knowledge in this article: