Create Assets Folder
Create new folder named assets in root project. In this folder, create new folders as below:
Images Folder
Create new folder named images in assets folder.
AutoLoad Url Helper
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');
Set BASE 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 Controller
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);
}
}
}
Set Default Controller
In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'demo';
Create View
Create new folder named demo in views folder. In this folder, create new views as below:
Index View
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>
Success View
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>
Structure of CodeIgniter Project
Run Application
Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index
Output
Select a image and upload file
Output
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- CodeIgniter Web Application Blueprints
- CodeIgniter for Rapid PHP Application Development: Improve your PHP coding productivity with the free compact open-source MVC CodeIgniter framework!
- Programming with CodeIgniter MVC
- Murach’s PHP and MySQL (3rd Edition)
- Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
- PHP and MySQL Web Development (5th Edition) (Developer’s Library)