Advertisement
  1. Code
  2. PHP
  3. CodeIgniter

Easy Development With CodeIgniter

Scroll to top
32 min read

In this week's 30 minute screencast, I'm going to show you how easy it is to work with the MVC pattern and CodeIgniter. This video is aimed at beginners who have no experience with a PHP framework.

For demonstration purposes, we'll be building a simple image upload utility. We'll then perform some validation, save the file to our uploads folder, and automatically create a respective thumbnail. With raw PHP, this can be somewhat time-consuming. However, with CodeIgniter, it's simply a matter of referencing the correct library, and passing in some configuration options! Let's dive in.

The Tutorial

Final Controller

1
<?php
2
3
class Upload extends Controller {
4
	
5
	function Upload() {
6
		parent::Controller();
7
		// $this->load->helper('form');

8
	}
9
	
10
	function index() {
11
		$this->load->view('upload_form');
12
	}
13
	
14
	function doUpload() {
15
		$config['upload_path'] = 'uploads/';
16
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
17
		$config['max_size'] = '1000';
18
		$config['max_width'] = '1920';
19
		$config['max_height'] = '1280';						
20
		
21
		$this->load->library('upload', $config);
22
		
23
		if(!$this->upload->do_upload()) echo $this->upload->display_errors();
24
		else {
25
			$fInfo = $this->upload->data();
26
			$this->_createThumbnail($fInfo['file_name']);
27
			
28
			$data['uploadInfo'] = $fInfo;
29
			$data['thumbnail_name'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];
30
			$this->load->view('upload_success', $data);	
31
		}
32
	}
33
	
34
	function _createThumbnail($fileName) {
35
		$config['image_library'] = 'gd2';
36
		$config['source_image'] = 'uploads/' . $fileName;	
37
		$config['create_thumb'] = TRUE;
38
		$config['maintain_ratio'] = TRUE;
39
		$config['width'] = 75;
40
		$config['height'] = 75;
41
		
42
		$this->load->library('image_lib', $config);
43
		if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
44
	}
45
}

Final View

1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
 
3
<html>
4
  <head>
5
    <title>Upload an Image </title>
6
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7
  </head>
8
  
9
  <body>
10
    <div id="container">
11
    	<h2>Upload an Image </h2>
12
13
		<?php echo form_open_multipart('upload/doUpload'); ?>
14
		<input type="file" name="userfile" />
15
		<p><input type="submit" value="Submit" name="submit" /></p>
16
		<?php echo form_close(); ?>
17
    </div>
18
19
  </body>
20
</html>

I hope you guys enjoyed this video tutorial. If you'd like to see more CodeIgniter tutorials and videos on Nettuts+, please be loud in the comments. I know I'd like to see more! I'm in the process of learning this framework myself, so links to resources, tips, etc. will be much appreciated!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.