Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help with PHP script to list directory needs to be recursive

Tags:

php

apache

I have a php script that reads a directory and lists all the files/directories in link form, and it works, except I'm trying to get it to be recursive so that when I choose another directory it once again uses the script to display the files inside. Right now when I select a directory it just goes to the default apache listing....any help is appreciated.

Script:

    <?php
$dirname = '/drives/Storage/AppsOSs/';
$webdirname = '/AppsOSs'; // what the directory appears as from the web browser's point of view

$dir = opendir($dirname);
$file_list = '';

while(($file = readdir($dir)) != false) {
if(($file != '.') && ($file != '..')) {
$file_list .= "<a href=\"$webdirname/$file\">$file</a><br/>";
}
}

closedir($dir);
?>

<p>
<?=$file_list?>
</p>
like image 596
jz3 Avatar asked Jul 08 '10 14:07

jz3


1 Answers

Take a look at the Directory Iterator and the Recursive Iterator

http://php.net/manual/en/class.directoryiterator.php

or even the Recursive Directory Iterator

http://www.php.net/manual/en/class.recursivedirectoryiterator.php

like image 102
Lizard Avatar answered Sep 25 '22 16:09

Lizard