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
<?php
class Upload extends Controller {
function Upload() {
parent::Controller();
// $this->load->helper('form');
}
function index() {
$this->load->view('upload_form');
}
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();
$this->_createThumbnail($fInfo['file_name']);
$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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Upload an Image </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<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(); ?>
</div>
</body>
</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!
- Follow us on Twitter, or subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.
- Bohonyi Balazs Zsolt
- http://www.mindred.co.uk Steve
- Bohonyi Balazs Zsolt
- http://www.tkinnovation.com Thad
- http://www.dobrotka.sk Tomas
- luka
- http://samiuljahan.wordpress.com samiul
- Imamu Hunter
- http://lbnuke.com Lori
- JDavis
- http://www.erepublik.co.uk Nelsaidi
- Shaun
- http://www.erepublik.co.uk Nelsaidi
- http://bigblupixl.com Mike
- http://net.tutsplus.com Jeffrey Way
- Arafin
- Kevin
- http://netnik.com Bob Hay
- Karl
- http://www.webcoursesbangkok.com Carl – Web Courses Bangkok Instructor
- misieg7
- pachito86
- http://www.demogeek.com DemoGeek.com
- http://www.demogeek.com DemoGeek.com
- Jerichvc
- http://www.diigital.com Mike Healy
- http://www.bluebit.co.uk Mark Jones
- http://www.bluebit.co.uk Mark Jones
- andrew
- Gustav
- http://timtocci.spaces.live.com/ Timothy Tocci
- http://www.masoutreach.nl mas
- Szymon W.
- http://www.suciuvlad.com Suciu Vlad
- Shovan
- http://www.daveredfern.com Dave Redfern
- Akmal Fikri
- artmania
- artmania
- Mark
- Derek
- http://altzgamer.ru Alex
- Ric
- http://www.code-pal.com Sumeet Chawla
- Dinesh
- Shiro
- Rick
- Babsvik
- Dratutsmits
- Kris
- Khalil
- Ryan Moss
- http://www.code-pal.com Sumeet Chawla
- http://www.aviank.com Anky
- http://websitecenter.ca/ Iouri Goussev
- http://blog.omarxp.web.id omarxp
- Phil
- http://www.codelobster.com Stas
- http://pinoydroid.net unwiredtech
- Sasdhar

