Thursday, October 11, 2012

How to List A Directory Contents Using PHP (Recursive)

function listDirectory($path)
{
    $handle = @opendir($path);
    while (false !== ($file = readdir($handle))) {
        if ($file == '.' || $file == '..') continue;
        if ( is_dir("$path/$file")) {
            echo "$path/$file\n";
            listDirectory("$path/$file");
        } else {
            echo "$path/$file\n";
        }
    }
     closedir($handle);
}
listDirectory("tutorials");