PHP traverses and prints all the file instances in the specified directory

Source: Internet
Author: User

  This article mainly introduces the PHP traversal and print all the file instances under the specified directory, you need friends can refer to the following

  Code as follows: <?php//function: Traverse and print all files   function Scan_dir ($dir _name, $dir _flag=1) { static $FILE _count=1 in the specified directory;                //record file number 1 directory name does not remember   $FILE _count--;                      //per call Scan_dir () function minus 1  @ $dir _handle=ope Ndir ($dir _name);    //Suppress error message display   Facilitate custom error display  if (! $dir _handle)  die ("Directory Open Error! ");  while (false!== ($filename =readdir ($dir _handle))  //The file name is ' 0 ', Readdir returns false to determine if the return value is not congruent  {    $flag = $dir _flag;                //Wacky Is_dir ($filename)! $filename This path must be able to find! Returns false   if ($filename!= '. ') when $filename does not exist or is not a directory. && $filename!= ' ... ')   {    $FILE _count++;                  //Do not record current path and previous path    while ($flag >0&&--$flag)          //negative number is still true    echo ';    if (Is_dir ($dir _name. $filename))  //determine if it is a directory    {    echo ' <strong> '. <a href= ". $dir _name. $filename." > ". $filename." </a></strong><br> ";     Scan_dir ($dir _name. $filename. ' /', $dir _flag+1);      //$dir _flag logo directory tree hierarchy   &nbsp    else    {    echo "<a href=". $d Ir_name. $filename. " > ". $filename." </a><br> ";   &NBSP}  }  }  closedir ($dir _handle);                //close directory handle  echo "Total Files:". $FILE _count. ' <br> '; }   Scan_dir (' d:wampwwwtestlamp61 ');  //the specified file path?>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.