PHPBuilder - Simple directory listing function



RSS Twitter
Snippets File Management

Simple directory listing function

by: Eric
|
April 19, 2006

Version: 0.0.1

Type: Function

Category: File Management

License: GNU General Public License

Description: A simple function that will list all files in a given directory that are not specified in the $ignore array.



<?php

function list_dir($dir, $ignore = array())
{
  //Open are directory
  $handle = opendir($dir);

  if($handle)
  {
    //Grab our files - Only the ones that do not match our
    //ignore array, etc.
    while(false !== ($file = readdir($handle)))
    {
      if($file != '.' && $file != '..' && !in_array($file, $ignore))
      {
        $files[] = $file;
      }
    }
    sort($files);
    $list = '';

    foreach($files as $file)
    {
      $list .= $file.'<br>';
    }
  }
  //Close directory
  closedir($handle);

  return rtrim($list, '<br>');
}

/*
Takes two arguments $dir & $ignore
$dir = the directory you want to list
$ignore = an array of specific files you don't want listed

Example usage:
*/

echo list_dir("some/directory/", array("includes", "someotherfile.php"));

/*
Outputs something like:

about.php
contact.php
index.php
style.css

... you get the idea ;)
*/

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Eric

Comment:



Comment:

(Maximum characters: 1200). You have characters left.