Easy Development With CodeIgniter
Jun 25th in Screencasts by Jeffrey Way
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.
Hi, I'm Jeff. I'm the editor of Nettuts+, and the Site Manager of Theme Forest. I spend too much time in front of the computer and find myself telling my fiance', "We'll go in 5 minutes!" far too often. I just can't go out to dinner while I'm still producing FireBug errors...drives me crazy. During my free time, I sporadically write articles for my own personal blog. If it will keep you in the good graces of the church, follow us on Twitter.
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!
User Comments
( ADD YOURS )Nathan Ledet June 25th
Awesome. I LOVE CodeIgniter tutorials.
( )skunkbad June 25th
My site is currently “powered by CI”, and I’d like to see more CI tutorials here!
( )Myfacefriends June 25th
another awesome CI tuts! thanks jeff!
( )Bernardo June 25th
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 June 25th
CodeIgniter is built to support PHP4, so no auto-loading.
( )Raymond Smith June 25th
Kohana is built from CodeIgniter.
Evan Riley June 25th
Thank you for such amazing tutorial on CodeIgniter, maybe you should start a CI series *wink*wink*
( )wayno007 June 25th
I second that!
( )begs June 26th
*signed*
( )Isaac Gonzalez June 26th
More CI tutorials or else I will say – NEE!!! : )
( )choise June 25th
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 June 25th
coz it’s easy peasy japanesey
( )Jeffrey Way June 25th
Haha.
Shaun June 25th
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 June 29th
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.
( )iPatrick June 25th
That’s Great ! I’ve never heard about CodeIgniter before
’till this time !
( )Kevin Urrutia June 25th
Awesome tut
( )crysfel June 25th
good!! very good, i like it, thanks for share
( )Srigi June 25th
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.
( )Shane Calhoun June 25th
Awesome Tut, would love to see more CI tuts using the models as well. Thanks!
( )John June 25th
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 June 25th
Good tutorial! =)
More, please!
Also, what is the name of the font of tTextMate?
( )Poor prince June 25th
Thanks 4 sharing…
CI is very nice framework yet.
I’m will uses C I for my next project.
1 more, thank you
( )Dizzledorf June 25th
I hope Insic Designs has an opinion on this…
( )Jeffrey Way June 25th
I believe insic is now myfacefriends (see above). Am I wrong?
( )r_jake June 25th
Not unless she’s aged about 20 years!
Myfacefriends June 25th
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 June 26th
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 June 26th
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 June 29th
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 June 25th
Haha, good stuff.
( )Felix Boyeaux June 25th
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 June 25th
Wow, I was just getting started with codeigniter… when i found this.
I would defiantly like to see more codeigniter tutorials!
( )Mike June 25th
Just getting started with CI. Great tut, would love to see more like this.
( )Lamin Barrow June 25th
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.
( )praveen June 25th
Thanx for the tutorial.
Can you tell me which software you use to capture the screencast?
( )Jeffrey Way June 25th
It’s called iShowU for Mac.
( )Dan Harper June 25th
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
Jeffrey Way June 25th
I don’t record at hi-res because it increases the file size far too much.
fil June 25th
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.
( )Media Contour June 25th
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 June 25th
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)
( )Chris Coyier June 25th
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 June 25th
Very useful, thanks.
( )JW where did you get that Wallpaper from, it’s pretty nice
Jeffrey Way June 25th
Just a Google search.
( )Mike June 25th
Loving the surge of CI tutorials, keep’em coming!
( )Omulet June 25th
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 June 25th
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 June 26th
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 June 26th
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.
( )Atroxide June 25th
Keep the CI tuts coming.
They own!
( )Bdesign June 25th
You’ve been a bad boy. “Now let’s delete the license” RIP
) . Nice tut, glad I’ve subscribed.
( )Latavish June 25th
Excellent Tutorial! Im a heavy CI user and hope to see more CI tutorials to come!
( )Muhammad Adnan June 25th
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 June 26th
Haha. You could make a philosophy of that idea! Object orientated problemsolving
( )IRV June 25th
Ok, you convinced me, let’s try CI =D
( )At first sight, the folder structure looks similar to rails.
charlesvallieres June 25th
PHP4 isn’t supported, why don’t you use Kohanna, php5?
( )cahva June 28th
It doesnt meant that CI does not support PHP5
( )Thomas June 25th
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 June 25th
Also CakePHP forms are much easier, it works out what type of input for you.
( )Jeffrey Way June 25th
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 July 11th
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.
( )Gene June 25th
thanks for another CI tutorial.. cheers!
( )Montana Flynn June 25th
These are my favorite tuts, lets keep em coming.
( )awake June 25th
I’m not a big fan of CI…
More Yii Framework tutorials (or) Flourish
- http://www.yiiframework.com/
( )- http://flourishlib.com/
Thorpe Obazee June 25th
Why post something irrelevant?
( )Sumesh June 25th
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
( )Yoosuf June 25th
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/
Fish June 25th
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!
( )Lamin Barrow June 27th
Check out this one here by Jonathan Snook http://snook.ca/archives/php/codeigniter_vs_cakephp/
( )Dj June 25th
Finally, where you been? This was great, as usual.
( )arnold June 25th
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!
digitalbart June 25th
go into itunes and you can download the episode from there usually
( )arnold June 27th
“please psdtuts I hope you can change this…”
( )I mean nettuts…just typo error
@digitalbart thanks!I will try to go there
digitalbart June 25th
More code igniter tuts here
http://eeinsider.com/blog/codeigniter-jquery-and-json-screencast/
( )Stevie Benge June 25th
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…
( )skunkbad June 25th
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 June 25th
I am more of a Cakephp person, but it’s always good to learn other things.
( )Eshban Bahadur June 25th
Not working if i use multiple “Browse” buttons for file upload with different name.
can you plz check?
( )Evan July 3rd
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.
( )luddo June 25th
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 June 25th
Sweet CI Rocks!
( )Philo June 25th
Great tutorial Jeffrey!
( )Bohonyi Balazs Zsolt June 26th
http://www.binarycake.com
They are two CodeIgniter gurus, and just started this site.
( )Steve June 26th
I vote for www. CI-TUTS.com
( )Bohonyi Balazs Zsolt June 26th
http://www.eeci2009.com/
( )Thad June 26th
Excellent Job Jeff. Thanks
( )Tomas June 26th
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
( )luka June 28th
You can check upload class and change do_upload(”userfile”) argument there in whatever you want
( )samiul June 26th
Hi Jeffry, It’s awesome tuts, hope to see more tuts about CI
( )Imamu Hunter June 26th
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.
( )Lori June 26th
Really enjoying the CI tutorials, but have switched over to Kohana. Any chance of some Kohana tutorials?
( )JDavis June 26th
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.
( )Nelsaidi June 27th
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.
( )Shaun June 27th
It might be a good idea to do a small project with CI before you do a large one. Otherwise you’ll most likely end up re-writing your code.
( )Nelsaidi June 29th
I’m rather experienced with PHP though, I have to admit first proper project was small, ended up rewriting aswell, thing is I like coding for a use, I have no ’small’ things to do with CI, so might aswell go ahead, was going to do it with raw php anyways, so CI would be much better.
Mike June 27th
shaun is complete right.
( )Jeffrey Way June 27th
Agreed.
Arafin June 27th
Very helpful, great attempt. thank you and hope to get more CI tutorials.
( )Kevin June 27th
Loved it. I would like to see more tutorials with codeIgniter.
( )Bob Hay June 27th
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.
( )Karl June 28th
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!
( )Carl - Web Courses Bangkok Instructor June 28th
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
( )misieg7 June 29th
CI sucks. It doesn`t even support tables associations. So lame.
( )pachito86 June 29th
Really cool and clear tut! I’ve always like CI and this tut shows its power!
( )DemoGeek.com June 30th
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.
( )DemoGeek.com June 30th
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.
( )Jerichvc June 30th
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.
( )Mike Healy July 1st
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.
( )Mark Jones July 3rd
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.
( )Mark Jones July 3rd
I solved this issue, I was using PHP5 and not PHP4. Could anyone explain why this was causing a problem?
( )andrew July 3rd
Great tutorial..
( )btw your desktop background kicks ass
Gustav July 6th
Very good tutorial!
Looking forward seeing another one.
greets from Sweden.
( )Timothy Tocci July 6th
Great tutorial – Keep ‘em coming!
( )mas July 9th
We’re using CI a lot and we love it.
( )Great tutorial for beginners! Keep up the good work!
Szymon W. July 9th
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?
Suciu Vlad July 14th
It’s called short-hand, to use it you must activate it in php.ini (short_open_tag). Anyway, as you can see, the short-hand method has bad portability.
( )Shovan July 10th
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
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!!
keep dem coming!!!
ps:My english is kind of bad ..sorry for dat
( )Dave Redfern July 16th
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.
( )Akmal Fikri July 21st
Kinda stuck somewhere.. Its said that I did not selected any file..
( )artmania July 23rd
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!
( )artmania July 23rd
okay, sorted. sorry for making mess at comments.
ps, thanks for great tut! exactly what i needed.
( )Mark August 5th
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 August 5th
“uploads/” is referring to a directory “uploads” in the root directory, not the Application directory.
( )Alex August 12th
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
( )Ric August 26th
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 !
( )Sumeet Chawla September 17th
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/
( )Dinesh October 7th
hai i am getting this error You did not select a file to upload.
( )plz help me urgent
Rick October 11th
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.
( )