HOME > > > > >

glob()

File_Find::glob()

ディレクトリ内で指定したパターンにマッチするものを検索します。

File_Find::glob(string $pattern, string $dirpath [, string $pattern_type = 'php'])

引数

$pattern
ディレクトリを検索する際のパターンを含む文字列を指定します
$dirpath
検索するディレクトリパスを含む文字列を指定します
$pattern_type
使用するパターンマッチング関数を含む文字列を指定します ('php' もしくは 'perl' もしくは 'shell' のいずれかが指定可能です).
$pattern_typeで指定した形式で$patternを記述する必要がある。「php」ならereg、「perl」ならpreg関数に準拠。基本的に処理速度の速いperlを指定すべき。詳しくは検索方法とそれらのパターン参照

戻り値

array
パターンにマッチする全てのファイル名とサブディレクトリを含む配列、 あるいは PEAR_Error。

サンプルコード

PHP

  1. <?php
  2. require_once('File/Find.php');
  3.  
  4. $dirinfo_lv1 = File_Find::glob('/.php$/', LIB_SAMPLE . 'file_find/', $pattern_type = 'perl');
  5. $dirinfo_lv2 = File_Find::glob('/.php$/', LIB_SAMPLE . 'file_find/lv2/', $pattern_type = 'perl');
  6. $dirinfo_lv3 = File_Find::glob('/.php$/', LIB_SAMPLE . 'file_find/lv2/lv3/', $pattern_type = 'perl');
  7.  
  8. var_dump($dirinfo_lv1);
  9. var_dump($dirinfo_lv2);
  10. var_dump($dirinfo_lv3);
  11. ?>
実行結果
array(4) { [0]=> string(8) "test.php" [1]=> string(9) "test1.php" [2]=> string(9) "test2.php" [3]=> string(9) "test3.php" } array(5) { [0]=> string(16) "lv2file_find.php" [1]=> string(8) "test.php" [2]=> string(9) "test1.php" [3]=> string(9) "test2.php" [4]=> string(9) "test3.php" } array(3) { [0]=> string(9) "test1.php" [1]=> string(9) "test2.php" [2]=> string(9) "test3.php" }