PHP CodeIgniter Framework – directory helper and directory_map()

7:25 PM in Programming by Vic Russell

CodeIgniter is my (current) favorite development platform for the PHP language.  It is lightweight, robust, and fairly easy to learn compared to Zend or Symfony.

As with every framework, there are issues with the documentation being too light or too heavy – seldom just right.  CodeIgniter documentation is excellent – once you get past the first couple of days digesting the syntax of the framework.

I was attempting to use the ‘directory’ helper.  This is a helper composed of a method that gets the names of files (and subdirectories) within a path and populates a multi-dimensional array with the results.  Two lines of code rather than around 10 for a pure PHP version that has the same features.

The key to getting this method to work was using the relative path and not an HTTP request.  If you use the base_url(), it will not work.  For my need, it was simply hard coding the path into my code:

$dirPath = "./this/is/the/directory/to/scan/";
$this->load->helper('directory');
$arrDirList = directory_map($dirPath);

Then, I used

foreach ($arrDirList as $key=>$value) { ... }

using the $key as the directory names to build my anchor tags and name the link.

foreach ($themeFiles as $key=>$value)                {

I had originally used a

$dirPath = $base_url() . "path/to/directory/"; // BAD X X X

Thanks, Vic Russell