CakeFest 2024: The Official CakePHP Conference

GlobIterator::count

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

GlobIterator::countディレクトリやファイルの数を取得する

説明

public GlobIterator::count(): int

glob 式から見つかったディレクトリやファイルの数を取得します。

パラメータ

この関数にはパラメータはありません。

戻り値

返されたディレクトリやファイルの数を int で返します。

例1 GlobIterator::count() の例

<?php
$iterator
= new GlobIterator('*.xml');

printf("Matched %d item(s)\r\n", $iterator->count());
?>

上の例の出力は、 たとえば以下のようになります。

Matched 8 item(s)

参考

  • GlobIterator::__construct() - glob を使うディレクトリを作成する
  • count() - 配列または Countable オブジェクトに含まれるすべての要素の数を数える
  • glob() - パターンにマッチするパス名を探す

add a note

User Contributed Notes 1 note

up
0
TwystO
7 years ago
As stated here https://bugs.php.net/bug.php?id=55701 the count() method can lead to errors.

For example this won't works if no files are found in the target directory :

<?php
$iterator
= new \GlobIterator($ftpDirectory . '/*.*', FilesystemIterator::KEY_AS_FILENAME);

if(
$iterator->count()) {
foreach(
$iterator as $filePath) {
// do some stuff ...
}
}
?>

A workaround to this bug could be :

<?php
foreach(new \GlobIterator($ftpDirectory . '/*.*', FilesystemIterator::KEY_AS_FILENAME) as $filePath) {
// do some stuff ...
}
?>
To Top