Get $500+ of the best After Effects files, video templates and music for only $20!
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!


Add Comment

Discussion 145 Comments

Comment Page 1 of 31 2 3
  1. Nathan Ledet says:

    Awesome. I LOVE CodeIgniter tutorials.

  2. skunkbad says:

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

  3. another awesome CI tuts! thanks jeff!

  4. Bernardo says:

    Good Tut.

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

  5. Evan Riley says:

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

  6. choise says:

    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 says:

      coz it’s easy peasy japanesey

    • Shaun says:

      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 says:

      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.

  7. iPatrick says:

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

  8. crysfel says:

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

  9. Srigi says:

    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.

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

  11. John says:

    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.

  12. Mini0n says:

    Good tutorial! =)
    More, please!

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

  13. Poor prince says:

    Thanks 4 sharing…

    CI is very nice framework yet.

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

    1 more, thank you

  14. Dizzledorf says:

    I hope Insic Designs has an opinion on this…

    • Jeffrey Way says:
      Author

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

      • r_jake says:

        Not unless she’s aged about 20 years!

      • 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.

      • insic says:

        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.

      • Jeffrey Way says:
        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 says:

        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!) :)

    • Jarryd says:

      Haha, good stuff.

  15. Felix Boyeaux says:

    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!

  16. Adrian says:

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

    I would defiantly like to see more codeigniter tutorials!

  17. Mike says:

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

  18. Lamin Barrow says:

    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.

  19. praveen says:

    Thanx for the tutorial.

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

  20. fil says:

    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.

  21. 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.

  22. Dixon Crews says:

    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)

  23. Chris Coyier says:

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

  24. Mikkel Liljegren says:

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

  25. Mike says:

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

  26. Omulet says:

    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.

    • Jeffrey Way says:
      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 says:

        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 says:

      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.

  27. Atroxide says:

    Keep the CI tuts coming.

    They own!

  28. Bdesign says:

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

  29. Latavish says:

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

  30. 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 .

  31. IRV says:

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

  32. charlesvallieres says:

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

  33. Thomas says:

    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 says:

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

      • Jeffrey Way says:
        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 says:

      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

  34. Gene says:

    thanks for another CI tutorial.. cheers!

  35. These are my favorite tuts, lets keep em coming.

  36. awake says:

    I’m not a big fan of CI…

    More Yii Framework tutorials (or) Flourish

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

  37. Sumesh says:

    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

  38. Yoosuf says:

    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/

  39. Fish says:

    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!

  40. Dj says:

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

  41. arnold says:

    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!

  42. Stevie Benge says:

    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…

  43. skunkbad says:

    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!

  44. James Barcellano says:

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

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

    can you plz check?

    • Evan says:

      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.

  46. luddo says:

    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.

  47. Brian says:

    Sweet CI Rocks!

  48. Philo says:

    Great tutorial Jeffrey!

Comment Page 1 of 31 2 3

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.