How PHP reads all filenames in directories and subdirectories

Source: Internet
Author: User
This article mainly introduces the PHP read directory and sub-directory of all the file name method, compared with several common methods, the last example summarizes a PHP read directory and sub-directory of all the file name method, very practical value, the need for friends can refer to, The implementation method is as follows:

In general, PHP read the directory under the file name in a lot of ways, the simplest is scandir, the specific code is as follows:

The code is as follows:

$dir = "./caxa/"; $file =scandir ($dir);p rint_r ($file);

Slightly more complicated, from the PHP manual:

The code is as follows:

$dir = "/etc/php5/";//Open a known directory, and proceed to the read its contentsif (Is_dir ($dir)) {if ($dh = Opendir ($dir)) {while ($file = Readdir ($DH)) {!== false) {echo "filename: $file: filetype:". FileType ($dir. $file). "\ n";} Closedir ($DH);}}

These can only read files in the currently specified directory, and files in subdirectories cannot be read. Originally wrote a loop to delete all directories of a piece of code, you need to delete all files, including multi-level subdirectories. But only need to read out the file name, a little more complex, the Internet to find a can use, the original code has error hints, changed the reference & $data place, as follows:

The code is as follows:

function Searchdir ($path,& $data) {if (Is_dir ($path)) {$DP =dir ($path), while ($file = $DP->read ()) {if ($file! = '). && $file! = ' ... ') {Searchdir ($path. ') /'. $file, $data);}} $DP->close ();} if (Is_file ($path)) {$data []= $path;}} function Getdir ($dir) {$data =array (); Searchdir ($dir, $data); return   $data;} Print_r (Getdir ('. '));
  • 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.