How do I view all the files (including subdirectories) in the directory? To kneel and beg God to help.
The existing code is as follows:
$handle =opendir ("./");
while (false!== ($file =readdir ($handle))) {
if ($file! = "." && $file! = "...") {
echo "$file
";
}
}
Closedir ($handle);
?>
The above code only lists the folder and file name in the directory where this php file resides, but does not display the file name in the folder under the directory.
Now like the catalogue (for example, not the real structure):
If this PHP is placed in folder A, how can I change it to show all the files under the A directory (including folders B, C, D, E, F, G, H, J, K)? Best sub-folder display!
By the way, it is best to add a judgment, if the file suffix is not HTML, it is displayed in red font.
To kneel and beg God to help. In this humbly! list file names
------Solution--------------------
A method of my own class, you change it yourself
Returns an array of two-dimensional
Parameter description:
$path requires an absolute path to the system with the end without the path character "\"
$size =true the number of bytes in the file.
$TOCSV = file name (string), the result of the return array is also written to this file, formatted in CSV format
* * Windows system cannot get path containing Unicode characters file name, this is not a program problem, is a problem with PHP
* * Do not list empty directories, only files, you can refer to the manual to recursivedirectoryiterator add a parameter to get an empty directory, but also note that getsize may be wrong
Public Function Listdir ($path, $size =false, $TOCSV =false)
{
if (false!== $toCsv) $NEWCSV = new Splfileobject ($TOCSV, ' WB ');
$items = new Recursiveiteratoriterator (new Recursivedirectoryiterator ($path));
$arr = Array ();
while (list ($itemName, $item) = each ($items))
foreach ($dir as $itemName = $item)
{
$fileInfo = Array ();
$fileInfo [' fullpath '] = (string) $itemName;
if ($size) $fileInfo [' size '] = (string) $item->getsize ();
if (false!== $toCsv) $newCsv->fputcsv ($fileInfo);
$arr [] = $fileInfo;
}
return $arr;
}
I have another class method that can get the Windows Unicode file name, but it is rather troublesome, and it is not posted
------Solution--------------------
function Show ($dir)
{
echo " {$dir}
";
$dir = Realpath ($dir). ' /*';
foreach (Glob ($dir, Glob_mark) as $f) {
if (substr ($f,-5) = = = '. html ') {
$f = "{$f} ";
}
Echo $f. "
". Php_eol;
if (Is_dir ($f)) {
Show ($f);