CodeIgniter - List of files and folders using opendir

Mike Lovely picture Mike Lovely · Mar 26, 2013 · Viewed 9.3k times · Source

I'm fairly new to certain programming techniques. Very new to OOP and MVC in general. And I'm pretty sure this is my first StackOverflow question!

I've just downloaded CodeIgniter and have a little project for myself.

I have a list of files and folders on the server and would like to use opendir, readdir and closedir etc to list out them out on a web page in ul's and li's - I've done this in procedural before in a function but have no idea where to even begin with CodeIgniter.

Is there a Helper or Library that already does this? If not, what is the best method? Should I put my code in the model folder?

So confused!

Answer

ruby picture ruby · Jan 9, 2015

I hope you have learned about MVC architecture already in past year :)

Now about your question. This helper or library you have asked for really exists. For the very same "problem" I have used directory helper and its function directory_map('relative/path/to/your/directory'). This function gets recursively content from your directory and sorts it into array like this

    Array
    (
    [banner] => Array
        (
        [0] => banner1.jpg
        [1] => banner2.jpg
        [2] => banner3.jpg
        [3] => banner4.jpg
        )
    [galerie] => Array
        (
        [0] => 0-PB083393.JPG
        [1] => DSCN2897.JPG
        [2] => DSCN2908.JPG
        [3] => DSCN2917.JPG
        [thumb] => Array
            (
                [0] => 0-PB083393_thumb.JPG
                [1] => DSCN2897_thumb.JPG
                [2] => DSCN2908_thumb.JPG
            )
        )
    [0] => mapa.jpg
    )

which is quite neat and you can use it in - for example - foreach cycle and add ul/li tags.

Probably this question is not relevant after one year, but I hope it could help others.