ディレクトリ内のすべてのファイルをループする PHP スクリプトですか? 質問する

ディレクトリ内のすべてのファイルをループする PHP スクリプトですか? 質問する

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.

ベストアンサー1

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());
    }
}
?>

おすすめ記事