PHP traversal of all files and folders in depth parsing _php tips

Source: Internet
Author: User
Tags strlen
1. Method One:
Copy Code code as follows:

?
$dir = "D:";
static $dir _list = 0;
static $file _list = 0;
function ListFile ($dir) {
Global $dir _list, $file _list;
$d = Dir ($dir);
while ($entry = $d->read ()) {
$tem _curnt= $dir. " /". $entry;
if ($entry = = "." | | $entry = = "...") Continue
if (Is_dir ($tem _curnt)) {
ListFile ($tem _curnt);
echo "folder". $tem _curnt. " <br> ";
$dir _list++;
}
ElseIf (Is_file ($tem _curnt)) {
echo "File". $tem _curnt. " <BR> ";
$file _list++;
}
}
$d->close ();
}
ListFile ($dir);
echo "Directory number:". $dir _list;
echo "<br>";
echo "Number of files:". $file _list;
?>

2. Method Two:
Copy Code code as follows:

<?php
function List_dir ($dirpath) {
if ($dirpath [strlen ($dirpath)]-1!= '/') {
$dirpath. = "/";
}
Static $result _array=array ();
if (Is_dir ($dirpath)) {
$files _dirs=scandir ($dirpath);
foreach ($files _dirs as $file) {
if ($file = = '. ' | | $file = = '.. ') {continue;}
if (Is_dir ($dirpath. $file)) {
List_dir ($dirpath. $file);
}else{
Array_push ($result _array, $dirpath. $file);
}
}
}
return $result _array;
}
$array =list_dir (' d:/www ');
foreach ($array as $value) {
Echo $value;
echo "<br>";
}
?>

3. Method Three:
Copy Code code as follows:

<?php
function List_dir ($dirpath) {
if ($dirpath [strlen ($dirpath) -1]!= "//") {$dirpath. = "//";}
Static $result _array=array ();
if (Is_dir ($dirpath)) {
$handle =opendir ($dirpath);
while ($file =readdir ($handle)) {
if ($file = = "." | | $file = = "...") {continue;}
if (Is_dir ($dirpath. $file)) {
List_dir ($dirpath. $file. " //");
}else{
Array_push ($result _array, $dirpath. $file);
}
}
Closedir ($handle);
}
return $result _array;
}
$array =list_dir ("d:/www");
foreach ($array as $value) {
Echo $value;
echo "<br>";
}
?>

4. Method Four:
Copy Code code as follows:

<?php
 function list_dir ($dirpath) {
  if ($ Dirpath[strlen ($dirpath) -1]!= "//") {$dirpath. = "//";}
  static $result _array=array ();
  if (Is_dir ($dirpath)) {
    $dir =dir ($dirpath);
   while ($file = $dir->read ()) {
    if ($file = = "." | | $file = = "...") {continue;}
    if (Is_dir ($dirpath. $file)) {
     list_dir ($dirpath. $ File. " //");
    }else{
     array_push ($result _array, $dirpath. $file);
    }
   }
    $dir->close ();
  }
  return $result _array;
 }
  $array =list_dir ("d:/www");
 foreach ($array as $value) {
  echo $value;
  echo "<br>";
 }
?>

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.