CodeIgniter from Scratch: File Operations
videos

CodeIgniter from Scratch: File Operations

Tutorial Details
  • Technology: CodeIgniter
  • Difficulty: Intermediate - Advanced
This entry is part 11 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

In today’s episode, we are going to be working with several helper functions, related to files, directories, and downloads. We are going to learn how to read, write, download files, and retrieve information about both files and directories. Also at the end we will build a small file browser that utilizes jQuery as well.

Catch Up

Day 11: File Operations

Get the Flash Player to see this player.
Final Example

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Carlos Caniguante

    Thank you!

    Burak

    Codeigniter rulez!!

  • Yusuf

    seriyi devam ettirdiğin için teşekkürler.

    -
    thanks

  • http://yloz.eu yloz

    @ 23:50 the video stop… :(

    thanx!

  • dave

    Thanks for continuing the series.

    The video stops around the 23min mark, just as things start to get interesting.

    Will try again later.

    • dave

      It’s stopping around the 23:45 mark for me. Going to try another browser.

    • http://net.tutsplus.com Jeffrey Way

      Video file is fixed. Must have stumbled when we copied it over to our server. :)

      • http://laranz.com lawrence77

        Jeff Ur gravatar also stumbled :)

  • http://www.sandelius.nu Tobias

    Really nice tutorial! I really like CI but prefer Kohana, which is based on CI but is built in PHP5.

    • Sid

      CI 2.0 will be based on PHP5

      • Shiro

        [from: http://www.michaelwales.com/2010/03/codeigniter-2-0-and-mercurial-transition/

        Phil Sturgeon and Elliot Haughin have provided their run-downs of CodeIgniter changes as well. One of the biggest changes I missed was the fact that CodeIgniter 2.0.0 functionality is not guaranteed to work in PHP4 and by CodeIgniter 2.1.0, legacy features will no longer be guaranteed to work in PHP4 either.

      • http://www.nukleo.fr Erik

        CI already runs perfectly on PHP5… running on PHP4 is only an option

      • http://bwebi.com barat

        @Erik
        Well – not realy. If something is backward compatible with PHP4 then it’s not full PHP5 withh all its futures.
        PHP5 is compatible (in most) with PHP4 and that’s why CI works on it.

      • http://www.sandelius.nu Tobias

        Anyone knows when CI 2.0 is coming?

  • Abdullah Al Mamun

    CI rocks!
    Thanks for the series but where to download this part??

  • dave

    It stops at 23:50 and tried 3 different browsers safari, chrome, ie.
    The cache was cleared if needed and I waited until the entire video was cached before starting the video around 22:00 mark.

    Anyone else having problems?

    • Ian

      Still have problems with this one. The page says I need to get flash player but haven’t had a problem with any of the other ci from scratch ones.

      I had this problem a while back with some of the premium dl’s. It was just random ones.

      I found that when I have this problem I can use downloadthemall addon and they will download fine.

      Try the link above with downloadthemall. It works

  • http://www.webguide4u.com WebGuide4U

    Great series of post i am getting started to learn codeigniter. thanks for all

  • http://adrusi.com adrian

    is there a ci version of mkdir. I’ve been having some weird issues with mkdir since I upgraded my local server

  • eric

    same problem as Dave. Like a 25cent peep show just as it gets good the curtain draws. Where do I put my quarter in for more……

  • http://www.dennismars.com Dennis Mars

    I’ve been watching/reading all of the tutorials and I’m wondering how you make each page link like this:

    home – example.com/
    news – example.com/news/
    blog – example.com/blog

    All of this “example.com/index.php/site/news” stuff is cool, but I know that not one of my clients will want their links to look like that!

  • Kyle

    Nice job Burak! I really like your tutorials you explain things very well. What font is it you are using in aptana?

    • http://www.phpandstuff.com Burak
      Author

      It’s Consolas

  • Jerichvc

    where to download video.

  • http://www.woony.be woony

    Great,
    still learning CI.
    Thanks for this

  • Tom Van Assche

    Fantastic, thank you!

  • http://stvproductions.net S.T. Verschoof

    Hey,
    this podcast isnt available on itunes yet! Can somebody take care of that?

    Also I would like to see the CodeIgniter From Scratch Day 5 CRUD episode on itunes Podcasts! It still isnt there : (. Dont know if that’s possible, but I do hope so.

    Love what’s been done with the site!!!

    Nice work Burak on this screencast!

  • Brad

    Thank you Burak. Files were always a ball of confusion for me, now I see plenty of places i could use the the codeigniter functions with them

    Well done.

  • http://izulcybercafe.com izulcybercafe

    Nice Post. Thanks…Codeigniter is The Best Framework. Yeah..

    btw, where net.tutplus.com mobile version..???

  • http://www.davidrojas.net David Rojas

    Just a comment: it seems that DIRECTORY_SEPARATOR is not necessary for specifying portable file names. http://us2.php.net/manual/en/ref.filesystem.php#73954

    • http://www.phpandstuff.com Burak
      Author

      Yeah, if you use forward slash, paths still work on Windows for example.

      But I encountered some strange behavior when using some CI functions. Seems like it’s because CI’s code references DIRECTORY_SEPARATOR while dealing with file paths. They do things like trimming the slash at the end of a path, or split the path string into pieces at the slashes etc… And since that constant translates into backslash (on windows), and if you passed forward slash to a CI function, sometimes it won’t work.

  • Alex

    i really love this CI series. Thanks @Jeffrey and @Burak i’ve learned allot thanks to this series.

  • nveselinov

    Hello, very useful lesson! Could you tell me what Font enjoy, like a mac? I like and I want to have it.

    • http://www.phpandstuff.com Burak
      Author

      The font I use is Consolas.

  • Brad

    Out of curiosity, why didn’t you use APPATH for the path to the file location? Is there some reason for files that that constant might not be useful?

    • http://www.phpandstuff.com Burak
      Author

      APPPATH is for the application folder, and BASEPATH is for the system folder. Just a matter of preference.

  • http://magpielab.com Team Magpie

    Great one, thanks @Burak Guzel, its really nice to see more tutes on nettuts, CodeIgniter really an easy framework and why not kohana tuts?

    • http://www.phpandstuff.com Burak
      Author

      I would like to get into Kohana too, but CodeIgniter is great for beginners. Has good documentation and big community.

  • http://www.jordanwalker.net Jordan Walker

    Great information.

  • ratnushock

    Great tutorial Burak!

    I have one little suggestion for next tutorials.
    To print out arrays in a “human readable” format try using the second parameter of print_r function ( default is false ):

    echo ” . print_r($array, true) . ”;

    This way you will see the array as you see it when viewing page source code when using print_r without the second parameter.

    What about some XML-RPC for next chapter?
    Cya!

    • ratnushock

      HTMLEntities messed up my last comment, inside those quotes there’s a PRE tag.

      Sorry.

  • Şaban Ulutaş

    süpersin Burak, eline sağlık.
    sorry guys, i just want to thank to Burak in our mother language ;)

  • http://www.moppa.se Moppa

    I tried to modify this script to fit my needs but i have run into some problems, i think it is related to my limited Javascript knowledge.

    In the “foreach”-loop i want to add more things to the click event-listener. So i modified the call to look like this:
    span.click(function (){
    MyFunc($(this), files[x]);
    });

    but this didn’t really do what i wanted… I want to pass the remaining content of files into the function, but it seems like the content is beeing parsed when the button is clicked (and then files[x] is undefined).

    Can you understand my problem or do i need to be more specific?

    //Moppa

  • mehdy

    HI,
    thanks for all great tuts
    but about the download link, this is impossible for me and also some of my firends ( i asked them ) to download it, could you please upload it as previous days or somewhere else like rapidshare ?

    Thanks again

    • mehdy

      Just 2 hours after commenting , i could download it
      thanks

  • Stacky

    When will be next tutorial?

  • http://www.night40.com Mr-H

    the things becomes intersting now,
    please burak go ahead..

  • Max Schwanekamp

    Burak how are you getting the nice code CI completion in Aptana? That’s one small annoyance about CI, that no IDE will do code completion — I have Komodo and use mostly Nusphere, neither does completion for CI. Are you using the older php plugin or somehow getting the Eclipse PDT to behave? Maybe an idea for another CI tut.

  • Bill

    I am having a problem with the javascript. I use an external file and when I type in the

    var files = ;

    I get an error

    Line: 36
    Character: 17
    Code: 0
    Error Message: Syntax error
    URL: http://localhost/nettuts.com/js/scripts.js

    Can someone tell me why it works in the actual file but not in an external js file?

    Here is the actual code

    // Directory Map

    var files = ;
    var file_tree = build_file_tree(files);
    file_tree.appendTo(‘#files’);

    function build_file_tree(files) {

    var tree = $(”);

    for (x in files) {

    if (typeof files[x] == “object”) {
    var span = $(”).html(x).appendTo(
    $(”).appendTo(tree).addClass(‘folder’)
    );
    var subtree = build_file_tree(files[x]).hide();
    span.after(subtree);
    span.click(function() {
    $(this).parent().find(‘ul:first’).toggle();
    });

    } else {
    $(”).html(files[x]).appendTo(tree).addClass(‘file’);
    }

    }

    return tree;

    }

  • http://kobru.ru Mark

    Nice n easy………exactly what I was looking for !!

  • Martin

    After playing around for a while I still can’t find a way to get the file URLs into my page to create download links like Burak suggested.

    I tried the function get_dir_file_info(BASEPATH) and successfully got the raw output of that into my HTML, but there is a major problem with that function: because it uses the filenames as array indexes, data is overwritten when you have duplicate filenames in the tree (e.g. index.html in many directories).

    Does anybody know what other function or method I should be using in CodeIgniter to get the paths for all my files into an array I can use to build my hrefs? Or do I have to do all the recursion and array-building manually (thus negating the need for CI)?

  • Rui Palmeira

    Hello there! nice series of tutorials on this framework, when I stumbled upon CodeIgniter I thought “who the hell would want a framework for php, in which you have to adapt to a kind of object oriented scripting language ?” then I saw this, I’m downloading all episodes as I’m writing this, and have one request if it can be fulfilled: how about a clean Url tutorial? I’ve read the mod_rewrite on CodeIgniter’s wiki, but it’s much better to have one of you guys who understand this things to explain on a video tutorial.

    peace & thanks for opening my eyes

  • Craig

    Why are you putting all the javascript in the body. Surely it should go in the head.

  • rampal

    hi i got very good idea of codeIgniter from these tutorial .Burak should try to cover more database stuff in examples

  • meltz

    How can we use the php function if the javascript and css are on an external file ?

  • murphy

    quick question.

    First thank you very much for this. I am trying to create something almost exactly to what this explains.

    second How would i go about turning the files into hyperlinks to download. I have been trying to use the force_download and download function in the controller from my view but cant seem to get it to work. has anyone else been able to get this to turn into a download hyperlink for the files?

  • Francky

    Hello Burak,

    I have thoroughly enjoyed your CI lessons. I’ve learned and continue learning so much.

    Could you please tell me how you get html:5 to write the html stuff for you?

    I have the newest version of Aptana and I’m a new Aptana user.

  • Levi

    The video is down. can you guys please re-upload it.

  • http://none charlie

    Hi all! for some reason, the icons burak include in the images folder are not appearing when I call the ‘ display ‘ function, the other functions work perfectly. Image with output of ‘ display ‘ function http://imageshack.us/photo/my-images/163/screenshotur.png/

    Also, I am using ubuntu, but I don’t think the problem is security related, because I can edit the hello.txt file(I set the permissions to read/write).

    P.S. : Well structured lesson, Burak. Keep up the good work!

  • http://egza.org farhad

    video is missing again

  • willycables

    The solution for view the route is in the jquery sentences, for example, if you click in the file, you can see the route in alert dialog.

    $(document).ready(function() {
    var files = ;
    var file_tree = build_file_tree(files);
    file_tree.appendTo(‘#files’);

    function build_file_tree(files) {
    var tree = $(”);
    for (x in files) {
    if (typeof files[x] == “object”) {
    var span = $(”).html(x).appendTo(
    $(”).appendTo(tree).addClass(‘folder’)
    );
    var subtree = build_file_tree(files[x]).hide();
    span.after(subtree);
    span.click(function() {
    $(this).parent().find(‘ul:first’).toggle();
    });
    }else{
    var li = $(”).html(files[x]).appendTo(tree).addClass(‘file’);
    li.click(
    function() {
    var route = $(this).parents(“.folder”).map(
    function(val,i) {
    return $(i).find(‘span’).html();
    }).get().reverse().join(“/”);
    route=route + “/” + $(this).html();
    if (route.substr(0,1)==”/”) route=$.trim(route).substr(1,route.length);
    alert(route);
    });
    }
    }
    return tree;
    }
    });

    Thaks Burak!

  • http://www.darknessfalls.org.uk Ged

    freezes at 33:28; two different browsers

  • https://twitter.com/mmdush Dushmantha

    You just made my day !!!! Thanks a lot for these tips :)

  • DOGS

    Oh god… how I lol’d at having to use DIRECTORY_SEPARATOR

    PHP sure is dumb, just translate CORRECT unix slashes to INCORRECT windows backslashes when ran in that moronic OS