How do I view all the files (including subdirectories) on the directory? Begging for help from the great God

Source: Internet
Author: User
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);
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.