假设我的目录看起来像:
ABC |_ a1.txt |_ a2.txt |_ a3.txt |_ a4.txt |_ a5.txt
您可以使用
glob()功能:
例01:
<?PHP // read all files inside the given directory // limited to a specific file extension $files = glob("./ABC/*.txt"); ?>
例02:
<?PHP // perform actions for each file found foreach (glob("./ABC/*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?>
例03:使用RecursiveIteratorIterator
<?PHP foreach(new RecursiveIteratorIterator( new RecursiveDirectoryIterator("../")) as $file) { if (strtolower(substr($file,-4)) == ".txt") { echo $file; } } ?>