Easy Development With CodeIgniter


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


class Upload extends Controller {
	function Upload() {
		// $this->load->helper('form');
	function index() {
	function doUpload() {
		$config['upload_path'] = 'uploads/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size'] = '1000';
		$config['max_width'] = '1920';
		$config['max_height'] = '1280';						
		$this->load->library('upload', $config);
		if(!$this->upload->do_upload()) echo $this->upload->display_errors();
		else {
			$fInfo = $this->upload->data();
			$data['uploadInfo'] = $fInfo;
			$data['thumbnail_name'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];
			$this->load->view('upload_success', $data);	
	function _createThumbnail($fileName) {
		$config['image_library'] = 'gd2';
		$config['source_image'] = 'uploads/' . $fileName;	
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		$config['width'] = 75;
		$config['height'] = 75;
		$this->load->library('image_lib', $config);
		if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();

Final View

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
    <title>Upload an Image </title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <div id="container">
    	<h2>Upload an Image </h2>

		<?php echo form_open_multipart('upload/doUpload'); ?>
		<input type="file" name="userfile" />
		<p><input type="submit" value="Submit" name="submit" /></p>
		<?php echo form_close(); ?>


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!