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.
- http://nathanledet.com Nathan Ledet
- http://www.brianswebdesign.com skunkbad
- http://myfacefriends.com Myfacefriends
- Bernardo
- Shaun
- Raymond Smith
- Evan Riley
- wayno007
- begs
- http://www.isaaccreative.com Isaac Gonzalez
- choise
- Sid
- http://hawyphp.com Hawyphp
- Shaun
- Tam
- http://www.ipatrick.hu iPatrick
- http://vivalacollege.com Kevin Urrutia
- http://www.quizzpot.com crysfel
- Srigi
- http://www.calhouncreations.com Shane Calhoun
- http://www.exoph.com John
- Mini0n
- http://www.indonesia.com Poor prince
- Dizzledorf
- r_jake
- http://myfacefriends.com Myfacefriends
- http://blog.insicdesigns.com insic
- Phil
- http://jarrydcrawford.com/ Jarryd
- Felix Boyeaux
- Adrian
- Mike
- http://laminbarrow.com Lamin Barrow
- http://apnerve.blogspot.com praveen
- http://danharper.me Dan Harper
- fil
- http://www.mediacontour.com Media Contour
- Dixon Crews
- http://css-tricks.com Chris Coyier
- Mikkel Liljegren
- Mike
- Omulet
- Omulet
- Cecily
- http://atroxide.com Atroxide
- http://bebliuc.ro Bdesign
- Latavish
- http://www.imblog.info Muhammad Adnan
- Fynn
- IRV
- charlesvallieres
- cahva
- Thomas
- Thomas
- Jordan
- Gene
- http://blog.complimedia.com Montana Flynn
- awake
- Thorpe Obazee
- http://designblurb.com Sumesh
- http://www.net-breeze.com Yoosuf
- http://www.lighthousetek.com Fish
- http://laminbarrow.com Lamin Barrow
- Dj
- http://www.twitter.com/arnold_c arnold
- http://digitalbart.com digitalbart
- http://www.twitter.com/arnold_c arnold
- http://digitalbart.com digitalbart
- http://superfancy.net Stevie Benge
- http://www.brianswebdesign.com skunkbad
- James Barcellano
- http://www.eshban.com Eshban Bahadur
- http://www.evanblack.com Evan
- http://twitter.com/_luddo luddo
- Brian
- http://www.philohermans.nl Philo

