How does PHP get a list of directories? How PHP recursively traverses files in a directory

Source: Internet
Author: User
Tags php programming
PHP How to get directory list, PHP recursively traverse the file in the specified directory method, in PHP programming using the directory function Opendir and Readdir, and combined with PHP recursive function to implement directory and file traversal operation.

I. How PHP gets a list of directories

PHP Get directory list :

Copy Codecode example: '." \ n "; }} closedir ($handle); }}}?>

Second, PHP recursive traversal of the specified directory files and statistics of the number of files method

PHP recursively iterates through the files of the specified directory and counts the number of files :

Example:

Copy Codecode example:

"; Read the current catalog file echo readdir ($dir). "
"; Read the parent directory file while ($filename =readdir ($dir)) {//To determine if the path under $dirname is a directory $newfile = $dirname. " /". $filename; The Is_dir () function determines whether the path of the current script is the directory if (Is_dir ($newfile)) {//the directory or file under its subdirectories is iterated through the recursive function ($newfile, $dirnum, $filenum); dirnum++; }else{$filenum + +;}} Closedir ($dir);}

$dirnum =0; $filenum = 0;

Total ("E:/appserv/www/phpmyadmin", $dirnum, $filenum); echo "Number of catalogs:". $dirnum. "
"; echo" Total Files: ". $filenum."
";//traverse the specified file directory and the number of files ends?>

  • 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.