Mike Lovely
Mike Lovely

Reputation: 21

CodeIgniter - List of files and folders using opendir

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!

Upvotes: 2

Views: 9993

Answers (4)

ruby
ruby

Reputation: 23

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.

Upvotes: 2

mikelovelyuk
mikelovelyuk

Reputation: 4152

Ha. This is funny. I was looking for something else and stumbled on to my first ever CI question without realising it was me :D

I've come so far with CI in just less than a month.

I found Directory Helper - directory_map that basically puts your folder structure in to an array of arrays.

I them created a recursive function in the model that turns it in to a proper drop down menu... And when it's a file, it adds in an a href link to that file.

http://ellislab.com/codeigniter/user-guide/helpers/directory_helper.html

Upvotes: 1

Kobus Myburgh
Kobus Myburgh

Reputation: 1202

First, welcome to CodeIgniter. It rules. Now...

You need a controller function to actually process the directory, similar to this:

public function dir_to_array($dir, $separator = DIRECTORY_SEPARATOR, $paths = 'relative') 
{
    $result = array();
    $cdir = scandir($dir);
    foreach ($cdir as $key => $value)
    {
        if (!in_array($value, array(".", "..")))
        {
            if (is_dir($dir . $separator . $value))
            {
                $result[$value] = $this->dir_to_array($dir . $separator . $value, $separator, $paths);
            }
            else
            {
                if ($paths == 'relative')
                {
                    $result[] = $dir . '/' . $value;                    
                }
                elseif ($paths == 'absolute')
                {
                    $result[] = base_url() . $dir . '/' . $value;
                }
            }
        }
    }
    return $result;
} 

Now you need to call that function to return the results, similar to:

$modules['module_files'] = $this->dir_to_array(APPPATH . 'modules');

This will put the results in a variable called $modules, which you can use in whichever way you want, typically put it in a view like this:

$this->load->view('folder/file', $modules);

If you provide an optional third parameter of TRUE to the load->view function, the result of that view will again be returned for you to use anywhere you like, otherwise it will be echoed out where you call it. The view may look something like this:

<?php

    if (isset($module_files) && !empty($module_files))
    {
        $out = '<ul>';
        foreach ($module_files as $module_file)
        {
            if (!is_array($module_file))
            {

                // the item is not an array, so add it to the list.
                $out .= '<li>' . $module_file . '</li>';
            }
            else
            {
                // Looping code here, as you're dealing with a multi-level array.
                // Either do recursion (see controller function for example) or add another
                // foreach here if you know exactly how deep your nested list will be.
            }
        }
        $out .= '</ul>';
        echo $out;
    }

?>

I have not checked this for syntax errors, but it should work fine. Hope this helps..

Upvotes: 0

Marc Audet
Marc Audet

Reputation: 46785

If I were doing this, I would:

(1) Create a Library class with a method that takes a directory name and returns an array of files.

(2) In my controller, I would then load the library and use it to get the list of files for the folder of interest.

(3) I would then load a view while passing the array of file names to the view where I would assemble the list.

Start by learning how to use the controller to load a view with data (start with a static array). Then learn how to create the library and integrate with your controller.

You might also read up about CodeIgniter's File Helper library unless you want to use the native PHP functions.

Also, learn about PHP unit testing.

I tend to use models for dealing with data from MySQL databases. In your case, you are dealing with information about your file system.

Good luck, CI is a good choice for a PHP/MySQL framework!

Upvotes: 0

Related Questions