| The code is as follows |
Copy Code |
|
<?php
$dirname = @opendir ("abc") Or Die ("Open Directory unsuccessful <br>");
Readdir ($dirname). " <br> ";
Readdir ($dirname). " <br> ";
while (($filess =readdir ($dirname))!=false)
{
if (Is_dir ("abc/". $filess))
{
echo "directory:". $filess. " <br> ";
}
Else
{
echo "File:". $filess. " <br> ";
}
}
Closedir ($dirname);
?> |
Can read only the HTML file name, can't read the directory name under the ABC directory, let's look at an example
| The code is as follows |
Copy Code |
|
<?php
/*
* Recursively gets all the files under the specified path or matches the specified regular file (excluding the.) and ".." ), the result is returned as an array
* @param string $dir
* @param string [$pattern]
* @return Array
*/
function File_list ($dir, $pattern = "")
{
$arr =array ();
$dir _handle=opendir ($dir);
if ($dir _handle)
{
This must be strictly compared, because the filename returned may be "0"
while (($file =readdir ($dir _handle))!==false)
{
if ($file = = = '. ' | | $file = = = '.. ')
{
Continue
}
$tmp =realpath ($dir. '/'. $file);
if (Is_dir ($tmp))
{
$RETARR =file_list ($tmp, $pattern);
if (!emptyempty ($RETARR))
{
$arr []= $RETARR;
}
}
Else
{
if ($pattern = = = "" | | preg_match ($pattern, $tmp))
{
$arr []= $tmp;
}
}
}
Closedir ($dir _handle);
}
return $arr;
}
Lists all files in the site root that end with the ". PHP" extension (case-insensitive)
Echo ' <pre> ';
Print_r (File_list ($_server[' document_root '), "//.php$/i"));
Echo ' </pre> ';
?>
This will be convenient for the infinite directory traversal. |