ディレクトリ内のファイルの数を数える PHP 質問する

ディレクトリ内のファイルの数を数える PHP 質問する

私は少し新しいプロジェクトに取り組んでいます。特定のディレクトリにファイルがいくつあるかを知りたいのです。

<div id="header">
<?php 
    $dir = opendir('uploads/'); # This is the directory it will count from
    $i = 0; # Integer starts at 0 before counting

    # While false is not equal to the filedirectory
    while (false !== ($file = readdir($dir))) { 
        if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
    }

    echo "There were $i files"; # Prints out how many were in the directory
?>
</div>

これまでのところ、これが私の得た情報です (検索による)。しかし、正しく表示されません。いくつかメモを追加しましたが、削除してもかまいません。メモは、私ができるだけ理解できるようにするためのものです。

さらに詳しい情報が必要な場合、または説明が不十分だと感じた場合は、遠慮なくお知らせください。

ベストアンサー1

次のように簡単に実行できます。

$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));

おすすめ記事