Easy Development With CodeIgniter
videos

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

<?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!


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://nathanledet.com Nathan Ledet

    Awesome. I LOVE CodeIgniter tutorials.

  • http://www.brianswebdesign.com skunkbad

    My site is currently “powered by CI”, and I’d like to see more CI tutorials here!

  • http://myfacefriends.com Myfacefriends

    another awesome CI tuts! thanks jeff!

  • Bernardo

    Good Tut.

    This kind of development could be lot easier using benefits of PHP5 auto_loading and Views treated as objects like in Kohana.

    • Shaun

      CodeIgniter is built to support PHP4, so no auto-loading.

      • Raymond Smith

        Kohana is built from CodeIgniter.

  • Evan Riley

    Thank you for such amazing tutorial on CodeIgniter, maybe you should start a CI series *wink*wink*

    • wayno007

      I second that!

    • begs

      *signed*

    • http://www.isaaccreative.com Isaac Gonzalez

      More CI tutorials or else I will say – NEE!!! : )

  • choise

    why is codeIgniter such popular in other Countrys like USA ?
    In Germany the mainpart of PHP Developer uses Frameworks like Zend, Symfony a.s.o

    • Sid

      coz it’s easy peasy japanesey

      • http://www.jeff-way.com Jeffrey Way
        Author

        Haha.

      • http://hawyphp.com Hawyphp

        loooooooool

    • Shaun

      If you have been on the CodeIgniter forums at all, you’d know that CI is used by people from all over the world, just as ZF is.

      Also, Zend is meant for developing highly scalable applications (typically medium/large-sized projects). CodeIgniter is scalable, but only to a point.

    • Tam

      I was doing some research and testing of frameworks a few years back, the frameworks in question were Symfony and CodeIgniter.

      I particularly didn’t like Symfony because of the step learning curve for the installation. Basically, I spent a couple of days trying to install the framework (which required an SSH/TELNET account, which is sometimes impossible due to some server configuration) and it got me nowhere and I know BSD and Linux well enough to configure and install applications and so on. So from that point on, I’ve dumped Symfony seeing that it was a little too difficult to install.

      I then took a look at CodeIgniter and what I particularly enjoyed was the video tutorials available on the site. Those videos just made the framework VERY easy to understand and it saved me a lot of time simply understanding the framework. Not only that, installing CodeIgniter is a joke and it can be summarized to “Download the package, uncompress the package, upload the package to hosting server, modify config and you’re live”. The documentation is very nice though sometimes slightly outdated.

      I haven’t had the opportunity to try Zend so I can’t really say anything about it but I’ve been hearing good stuff about it.

  • http://www.ipatrick.hu iPatrick

    That’s Great ! I’ve never heard about CodeIgniter before :( ’till this time ! ;)

  • http://vivalacollege.com Kevin Urrutia

    Awesome tut

  • http://www.quizzpot.com crysfel

    good!! very good, i like it, thanks for share :D

  • Srigi

    Thanks JW for screencast – this 30mins helped me to realize, that CI is framework not for me. No Offense, but Zend FW, Symfony or czech rising star “Nette” are better choices for me.

    No flame pls.

  • http://www.calhouncreations.com Shane Calhoun

    Awesome Tut, would love to see more CI tuts using the models as well. Thanks!

  • http://www.exoph.com John

    Awesome! I love these CI tuts. Out of all the frameworks there is, CI is by far my favorite and it’s not just because I’m from the US. I personally think that CI is a great framework. Hopefully we can see some more applications of CI in the future.

  • Mini0n

    Good tutorial! =)
    More, please!

    Also, what is the name of the font of tTextMate?

  • http://www.indonesia.com Poor prince

    Thanks 4 sharing…

    CI is very nice framework yet.

    I’m will uses C I for my next project.

    1 more, thank you

  • Dizzledorf

    I hope Insic Designs has an opinion on this…

    • http://www.jeff-way.com Jeffrey Way
      Author

      I believe insic is now myfacefriends (see above). Am I wrong?

      • r_jake

        Not unless she’s aged about 20 years!

      • http://myfacefriends.com Myfacefriends

        opss who’s insic? I’m Jehnee co-owner of http://myfacefriends.com/jehnee im here to learn mo new stuff… by the way jeff I’m learn a lot of things here. thanks more power.

      • http://blog.insicdesigns.com insic

        ahaha its funny Jeff that you try to say that myfacefriends is me(insic).

        and Dizzledorf I dont have much an opinion for this tut, as people know JW is the man. lol I havent watch the screencast yet im having a connection problem. But by taking a quick look at the code in the controller. I disagree in using directly the “echo”. :) Im sure JW has a solid reason why he is doing that.

      • http://www.jeff-way.com Jeffrey Way
        Author

        Hey Insic – Sorry…haven’t seen you around in a while.

        Are you referring to using “echo” instead of short tags — like < ?= 'some string' ?>

        I use both in the video tutorial.

      • Phil

        I think it’s where you’re using echo to show the upload error straight from the controller rather than displaying it in a view – lines 23 & 43.

        Great tut none the less (as always!) :)

    • http://jarrydcrawford.com/ Jarryd

      Haha, good stuff.

  • Felix Boyeaux

    I love how easy it is to produce outstanding web apps in no time using CodeIgniter. I just have some weird issues with redirecting… My code falls into a Redirect Loops pretty often.
    Anyway, great screencast Jeff!

  • Adrian

    Wow, I was just getting started with codeigniter… when i found this.

    I would defiantly like to see more codeigniter tutorials!

  • Mike

    Just getting started with CI. Great tut, would love to see more like this.

  • http://laminbarrow.com Lamin Barrow

    Codeigniter is really cool. The only reason keeping me away from it so far along with the other products from EllisLab is their licensing policies. Some how it just doesn’t appeal to me as a free and open-source software IMHO.

  • http://apnerve.blogspot.com praveen

    Thanx for the tutorial.

    Can you tell me which software you use to capture the screencast?

    • http://www.jeff-way.com Jeffrey Way
      Author

      It’s called iShowU for Mac.

      • http://danharper.me Dan Harper

        Jeff, you may want to look into ScreenFlow. I see you get slow-down (jumpiness) with iShowU, as did I when I tried it.
        However everything works very smooth, at full resolution with ScreenFlow :)

      • http://www.jeff-way.com Jeffrey Way
        Author

        I don’t record at hi-res because it increases the file size far too much.

  • fil

    The timing is perfect. I just created my 1st CI application 2 days ago! Easy easy. I’d love to see more CI tutorials and specifically about the inclusion of AJAX type features within CI apps.

    Thanks for the great tutorial.

  • http://www.mediacontour.com Media Contour

    This tutorial is really useful for beginners and seasoned developers alike. This is one of the easiest ways to design web apps that has been created thus far. Even designers can use it! I hope to see more applications like this in the future.

  • Dixon Crews

    Yes please, much more tuts on CodeIgniter!

    I’m building my whole website with it and for me, the best tut is the user guide. (Shocking)

  • http://css-tricks.com Chris Coyier

    Very nice. I’m playing with it right now. CodeIgnitor just might be the ticket I need to start actually writing some applications myself.

  • Mikkel Liljegren

    Very useful, thanks.
    JW where did you get that Wallpaper from, it’s pretty nice :D

    • http://www.jeff-way.com Jeffrey Way
      Author

      Just a Google search. :)

  • Mike

    Loving the surge of CI tutorials, keep’em coming!

  • Omulet

    My first comment on nettuts, and I regret that is not starting with “Oh my… awsome”.
    You are a good programmer Jeffrey, and I think this could be a great tut, … “Could” because I don’t think that somebody with no knowledge about codeIgniter will understand something from this? It will be dificult even for an advanced PHP programmer who did not heard about CI. No offence, but you should start with a basic description about the framework, maybe presenting some online resources about the classes you’re using, and how the developer can extend your code using them.

    Thanks, and good luck.

    • http://www.jeff-way.com Jeffrey Way
      Author

      This wasn’t meant to be a first look at CodeIgniter video. I recommend you review the CI website. They have a fantastic forum and user guide. This is specifically why I recommended that the viewer take some time to learn what MVC actually is.

      • Omulet

        I’m actually developing on cakePHP and ruby on rails, and the reason I was interested about this tut was to have a little analogy between cake and CI. Thanks for reply and keep up your good work.

    • Cecily

      I agree with Omulet. Saying that this is “Easy Development” with CodeIgniter presupposes a lot about the expertise of people who come here to try to learn things from the site. I’m not stupid, but I *am* a beginner, and one of my biggest complaints with many net tutorials (including this one) is that it assumes that a person already has a base level of knowledge.

      When I go to the CodeIgniter site, all I see is a bunch of code that gives me snippets, but no real suggestions/recipes for how I might use these snippets to actually build something. Someone needs to make the connection there. I hope that the next tutorial in the CI series will start to make that connection.

      That said, keep up the good work.

  • http://atroxide.com Atroxide

    Keep the CI tuts coming.

    They own!

  • http://bebliuc.ro Bdesign

    You’ve been a bad boy. “Now let’s delete the license” RIP :)) . Nice tut, glad I’ve subscribed.

  • Latavish

    Excellent Tutorial! Im a heavy CI user and hope to see more CI tutorials to come!

  • http://www.imblog.info Muhammad Adnan

    i will also take start now to learn CI. it looks alike . there is usage of classes and MVC.

    OOP is good . Imagine every problem of real word like a class and object .

    • Fynn

      Haha. You could make a philosophy of that idea! Object orientated problemsolving ;)

  • IRV

    Ok, you convinced me, let’s try CI =D
    At first sight, the folder structure looks similar to rails.

  • charlesvallieres

    PHP4 isn’t supported, why don’t you use Kohanna, php5?

    • cahva

      It doesnt meant that CI does not support PHP5 :)

  • Thomas

    Nice Tut,

    However I must say, I’m still hooked on CakePHP, gives me a nice warm feeling everytime I write code. =P

    I don’t know, but it seems that CI code is a bit trickier, with loading libraries like that. With CakePHP I just include them into a Class variable array at the top… Works for me.

    • Thomas

      Also CakePHP forms are much easier, it works out what type of input for you.

      • http://www.jeff-way.com Jeffrey Way
        Author

        I haven’t played around with CakePHP just yet. As far as the libraries, it’s really a simple matter of autoloading the ones you’ll need during the first 20 seconds of your project.

    • Jordan

      One problem I have with CakePHP is how the SQL’s are handled I used to love coding in Cake Personally, and as I went bigger and bigger into Cake I noticed more and more things that the Developers should and could have done better to improve Flexibility with CakePHP.

      Such as their queries, and deletes.. delete could have at least returned a Boolean Value haha.. I have discussed it with the Developers and they responded with, “We’re doing it right, you’re doing it wrong”. Which made me walk away from CakePHP Personally.

      I have been dabbling with PHP Frameworks here and there for a couple years now and haven’t found the right fit yet :(..

      I haven’t tried CI yet though, I have a Co-Worker who has used it a couple times in the past so I might try it out cause this Vid. :D

  • Gene

    thanks for another CI tutorial.. cheers!

  • http://blog.complimedia.com Montana Flynn

    These are my favorite tuts, lets keep em coming.

  • awake

    I’m not a big fan of CI…

    More Yii Framework tutorials (or) Flourish

    - http://www.yiiframework.com/
    - http://flourishlib.com/

    • Thorpe Obazee

      Why post something irrelevant?

  • http://designblurb.com Sumesh

    Good work, Jeff, I’d certainly like to see more CI tutorials. I’m just learning PHP, and found your Dive into PHP easy and useful.

    By the time I’m serious enough to use frameworks, these screencasts should help – hope I’m (along with others in this comment section) loud enough that we need more tutorials and screencasts :D

  • http://www.net-breeze.com Yoosuf

    cool jeff, after a long time a screen cast from you!, BTW for nest application you demostrate a ACL as like Andrew Steenbuck’s
    http://net.tutsplus.com/tutorials/php/a-better-login-system/

  • http://www.lighthousetek.com Fish

    Hey Jeff,

    Could I get you to weigh in your thoughts on CakePHP vs. CodeIgniter? I’m curious to know your thoughts, pros & cons, etc. You’re the man!

  • Dj

    Finally, where you been? This was great, as usual.

  • http://www.twitter.com/arnold_c arnold

    arggh!!! I wish I can download the video,because my internet connection speed isnt that good,
    please psdtuts I hope you can change this…

    anyway nice tutorial…Im still looking forward for more tuts about CI in here..
    CodeIgniter RULES!

    • http://digitalbart.com digitalbart

      go into itunes and you can download the episode from there usually

    • http://www.twitter.com/arnold_c arnold

      “please psdtuts I hope you can change this…”
      I mean nettuts…just typo error
      @digitalbart thanks!I will try to go there

  • http://digitalbart.com digitalbart
  • http://superfancy.net Stevie Benge

    I’m in the process of learning Codeignitor and MVC methodologies (particularly ASP.Net MVC framework) as well so I would certainly check out more CI screencasts. Of course I’ll check out just about any screencast you do, so thanks for that…

  • http://www.brianswebdesign.com skunkbad

    I commented earlier, but I want to add that I very much prefer the BSD KNF style compared to the Allman style that CI uses, and I’m glad you use it! It just seems more clear to me. Thanks!

  • James Barcellano

    I am more of a Cakephp person, but it’s always good to learn other things.

  • http://www.eshban.com Eshban Bahadur

    Not working if i use multiple “Browse” buttons for file upload with different name.

    can you plz check?

    • http://www.evanblack.com Evan

      Multiple uploads in CI are a different story and much more difficult. Look into the documentation about naming file fields something other than “userfile”. You have to configure it when you load the upload library.

  • http://twitter.com/_luddo luddo

    I recommend to use the orm datamapper for CI, very useful for manipulating data. Check it, the doc is very intuitive (like CI doc).

    nice tut btw Jeffrey.

  • Brian

    Sweet CI Rocks!

  • http://www.philohermans.nl Philo

    Great tutorial Jeffrey!