note 88868 added to function.readdir
| From: | ianatvirante dot com at osu1 dot php dot net | Date: | Wed, 11 Feb 2009 17:45:22 +0000 |
| Subject: | note 88868 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150501@lists.php.net to get a copy of this message | ||
I couldn't easily find a way to recursively search and list all the files and directories in a
directory. So here's a stab at it for anyone else who's looking.
<?php
//define the path as relative
$path = "path to directory";
$webpath ="the url you want to place before your filename/";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
list_dir($dir_handle,$path);
function list_dir($dir_handle,$path)
{
// print_r ($dir_handle);
echo "<ol>";
//running the while loop
while (false !== ($file = readdir($dir_handle))) {
$dir =$path.'/'.$file;
if(is_dir($dir) && $file != '.' && $file !='..' )
{
$handle = @opendir($dir) or die("undable to open file $file");
echo "<li><a href='$webpath.$file'>$file</a></li>";
list_dir($handle, $dir);
}elseif($file != '.' && $file !='..')
{
echo "<li><a
href='$webpath.$file'>$file</a></li>";
}
}
echo "</ol>";
//closing the directory
closedir($dir_handle);
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 66.93.243.50
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/88868
Del: integrated -- https://master.php.net/note/delete/88868/integrated
Del: useless -- https://master.php.net/note/delete/88868/useless
Del: bad code -- https://master.php.net/note/delete/88868/bad+code
Del: spam -- https://master.php.net/note/delete/88868/spam
Del: non-english -- https://master.php.net/note/delete/88868/non-english
Del: in docs -- https://master.php.net/note/delete/88868/in+docs
Del: other reasons-- https://master.php.net/note/delete/88868
Reject -- https://master.php.net/note/reject/88868
Search -- https://master.php.net/manage/user-notes.php