Easy Development With CodeIgniter
videos

Easy Development With CodeIgniter

Share

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!


Related Posts

Add Comment

Discussion 137 Comments

Comment Page 2 of 2 1 2
  1. Bohonyi Balazs Zsolt says:

    http://www.binarycake.com

    They are two CodeIgniter gurus, and just started this site.

  2. Steve says:

    I vote for www. CI-TUTS.com

  3. Thad says:

    Excellent Job Jeff. Thanks

  4. Tomas says:

    Thank you Jeffrey, nice Tutorial, i Like CI very much along with Symfony.

    I have a question… In the HTML form the file element is named ‘userfile’, but the element name ‘userfile’ is not mentioned anymore in the code. Is this needed to identify the uploaded file, or MORE files?

    Maybe you could explain this in your tutorial. Or explain the possibility of uploading more than one file. Is this later stored in array? Or can we identify the form element name that was used? Like the one called ‘userfile’ in this tutorial.

    Fine tutorial anyway, thank you, It took me one step closer to Code Igniter :)

  5. samiul says:

    Hi Jeffry, It’s awesome tuts, hope to see more tuts about CI

  6. Imamu Hunter says:

    This tutorial is awsome I was trying to get into ruby on the rails and switch from php but this will work wonders for me. thank keep them comming.

  7. Lori says:

    Really enjoying the CI tutorials, but have switched over to Kohana. Any chance of some Kohana tutorials?

  8. JDavis says:

    I must admit I’d heard of CodeIgniter but dismissed it as just another bloated framework.

    This great tutorial has convinced me to take another look into CI. The pacing and explanations (just enough not too much) were spot on. Keep up the great work!

    Oh and almost 500GB free on your mac? Must be a nice Mac Pro you’ve got there. :-)

  9. Nelsaidi says:

    Awesome, I’m about to start a rather large CodeIgniter project in the next few days, and this will greatly help especialy as I have no CodeIgniter experience, let alone any PHP framework.

  10. Arafin says:

    Very helpful, great attempt. thank you and hope to get more CI tutorials.

  11. Kevin says:

    Loved it. I would like to see more tutorials with codeIgniter.

  12. Bob Hay says:

    Very interesting tutorial. My impression is that CodeIgniter is to PHP what jQuery is to Javascript. Both are blowing my mind.

    Thank for the great tut.

  13. Karl says:

    Thank you Jeff! You make all the tutorials so easy to understand and follow. I would like to see more CI tuts. It’s a truly great framework.

    Thanks so much for your effort!

  14. Codeigniter is something I really wanted to get into, but never had the time. I have a few other things on my list to master before I get to this one, namely WordPress and Joomla as they both released recent updates.

    Great great tutorial as always.

    C

  15. misieg7 says:

    CI sucks. It doesn`t even support tables associations. So lame.

  16. pachito86 says:

    Really cool and clear tut! I’ve always like CI and this tut shows its power!

  17. DemoGeek.com says:

    Jeff – Is there any specific reason why you didn’t use the CodeIgniter helpers, like form_input instead of ? I would really like to know if there is a reason behind it.

  18. DemoGeek.com says:

    Of course, the tags got stripped…the question was – Is there any specific reason why you didn’t use the CodeIgniter helpers, like form_input instead of the input HTML tag? I would really like to know if there is a reason behind it.

  19. Jerichvc says:

    Jeff, can you show us a round up for using Models in codeigniter, screencast?
    current app is only using View & Controller in MVC.

    will wait for it.

  20. Mike Healy says:

    I’ve been meaning to get into Code Igniter too. It seems more attractive to me than other frameworks due to the shallow learning curve. The PHP4 compatibility is not ideal though. PHP4 really needs to be dumped, and continuing to support it with new projects does not help that end.

    The pace of this video tutorial fits well with CI’s get-stuff-done approach. It’s good that the video was free of verbal dawdling and unnecessary padding. Full screen quality was also good.

  21. Mark Jones says:

    This doesn’t appear to work for me even when I use the actual source, everything works except for the final creation of the thumbnail. I’m pretty sure GD is installed as I’ve installed MAMP but for some reason this is the one part that isnt working which is a shame as I have no idea why. Any ideas? If i take the final line of the createThumbnail function out everything else works, but if i put it back in i get a white screen.

  22. andrew says:

    Great tutorial..
    btw your desktop background kicks ass

  23. Gustav says:

    Very good tutorial!

    Looking forward seeing another one.

    greets from Sweden.

  24. Great tutorial – Keep ‘em coming!

  25. mas says:

    We’re using CI a lot and we love it.
    Great tutorial for beginners! Keep up the good work!

  26. Szymon W. says:

    Excouse my english :)

    Great tutorial Jeffrey. Thank you.
    All works for me except one thing. The view “upload_success.php” does not display the values from $data array – i had to change php tags from <?= to <?php and echo all the values – why is that? Is it my apache or ci configuration?

  27. Shovan says:

    Im from Bangladesh and we don`t have paypal here :( but! i enjoy this site more den anything!! if i was da site owner i would never have given all da tuts for 9 dollar! dis is crazy!!(no offense:P) but since im not manager!! !! im really happy dat i can still watch for free n learn :D I spend arnd 4hrs everyday learning things dat i thot i could never learn in my lyf..dis screen casts r life saverz ..teachin method is almost godly!! haha! jeff!!! im followin u every wer!! :P keep dem coming!!! :D

    ps:My english is kind of bad ..sorry for dat :)

  28. Dave Redfern says:

    Brilliant tutorial. Very informative and perfect speed for me to keep up coding for myself.

    Does anyone know when the next one is? or how often they will be released.

  29. Akmal Fikri says:

    Kinda stuck somewhere.. Its said that I did not selected any file..

  30. artmania says:

    wow great tutorial! exactly what I need! but i have a problem :(

    Why do I get “The upload path does not appear to be valid.” error all the time? I tried all possibilities about path, but still same!

    here is my exact path for project:
    $config['upload_path'] = ‘http://localhost/rl/uploads/’;

    thanks a lot for help! I appreciate!

  31. Mark says:

    Hi,

    lovely too, shows how easy it is to use CI. Got a problem tho … like the above gut, I get:
    ‘The upload path does not appear to be valid.’
    message.

    path in controller is:

    $config['upload_path'] = ‘uploads/’;

    and I’ve created a folder under application called uploads – so path is

    application/uploads

    anyone help?

    • Derek says:

      “uploads/” is referring to a directory “uploads” in the root directory, not the Application directory.

  32. Alex says:

    Hi, Jeff, amazing tutorial. I’ve got one question. How can I insert the values of an image and a thumnail into a database among other data in a form? Is there any shortcut?

    Thanks

  33. Ric says:

    This will be my first time using a php framwork. I stayed away because I am not a php guru and was afraid that a framework might hide important things away from me while learning, but the speed benefits outways my fears :)

    is there any way to save this screen cast so I can watch it in parts? here in Australia download limits are still an issue. is there a download section?

    I am a fresh plus member and I very glad I subscribed !

  34. I need to develop some site which I think is not possible by just using wordpress. Hence, I started researching on what should I use for development. The result I got was that I have to develop a custom web app to achieve my target :| Which in turn lead me to Code Ignitor. I was confident that nettuts is going to have at least a tutorial on it and to my surprise you guys have a whole series! NetTuts+ just totally rocks !! \:D/

  35. Dinesh says:

    hai i am getting this error You did not select a file to upload.
    plz help me urgent

    • Shiro says:

      please add the name of the input type to the parameter do_upload, then u can fix the problem

      if ( ! $this->upload->do_upload(‘file_Upload’) )
      {

      }

  36. Rick says:

    this may sound dumb but I have uploaded codeigniter to my server and can not seem to get it to run went to the index file in the root of the app and all I get is a page telling me where I could edit the page, nothing like any of the tutorials show.

  37. Babsvik says:

    Great tut Jeff.

    Love all this CI stuff, thanks!

    B

  38. Dratutsmits says:

    a usted la elecciГіn no fГЎcil http://nuevascarreras.com/ cialis generico espana Mi dispiace, ma, a mio parere, si sbaglia. Sono sicuro. Scrivere a me in PM, ti parla. cialis 20 mg

  39. Kris says:

    Great tutorial! Thank you so much!

  40. Khalil says:

    Dear Jeff,

    Awesome tutorial thanks.

    There aren’t good tutorials (Screencast) on the internet I have googled a lot of time for “Codeigniter” you are going awesome.

    Thank you for sharing easy and meaningful videos.

    I am waiting for more advanced screencasts…

  41. Ryan Moss says:

    Hey when previewing in the browser for the first time i get an error message and i cant fix it. Please help.

    The error message is:

    Fatal error: Class ‘Parent’ not found in C:\xampp\htdocs\Imageupload\application\controllers\Upload.php on line 6

  42. Hello Jeff,
    I found this tutorial on ‘September 17, 2009 at 2:15 am’ and today is when am actually doing it! Gosh.. talk about being lazy… :|

    Loved the way you taught it.. great job :)

Comment Page 2 of 2 1 2

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.