且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP脚本循环遍历目录中的所有文件?

更新时间:2023-09-17 15:58:28

你可以使用 DirectoryIterator 。来自php的示例手册:

 <?php 
$ dir = new DirectoryIterator(dirname(__ FILE__));
foreach($ dir as $ fileinfo){
if(!$ fileinfo-> isDot()){
var_dump($ fileinfo-> getFilename());
}
}
?>


I'm looking for a PHP script that loops through all of the files in a directory so I can do things with the filename, such as format, print or add it to a link. I'd like to be able to sort the files by name, type or by date created/added/modified. (Think fancy directory "index".) I'd also like to be able to add exclusions to the list of files, such as the script itself or other "system" files. (Like the . and .. "directories".)

Being that I'd like to be able to modify the script, I'm more interested in looking at the PHP docs and learning how to write one myself. That said, if there are any existing scripts, tutorials and whatnot, please let me know.

You can use the DirectoryIterator. Example from php Manual:

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}
?>