English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP glob() Function Usage and Example

PHP Filesystem Reference Manual

The glob() function can return an array containing file names or directories that match the specified pattern. This function can return an array containing matching files/an array of directories, otherwise it returns false.

Syntax

array glob ( string $pattern [, int $flags = 0 ] )

The glob() function can search for all path names that match the pattern used by glob(), which is similar to the rules used by a normal shell.

Example1

<?php
   print_r(glob("/PhpProject/php/*.txt");
?>

Output Result

Array
(
    [0] => /PhpProject/php/phptest1.txt
    [1] => /PhpProject/php/phptest2.txt
    [2] => /PhpProject/php/phptest3.txt
    [3] => /PhpProject/php/phptest4.txt
    [4] => /PhpProject/php/phptest5.txt
    [5] => /PhpProject/php/phptest6.txt
    [6] => /PhpProject/php/phptest7.txt
    [7] => /PhpProject/php/phptest8.txt
    [8] => /PhpProject/php/phptest9.txt
    [9] => /PhpProject/php/phptest10.txt
)

Example2

<?php
   foreach(glob("/PhpProject/php/*.txt") as $filename) {
      echo "\$filename size " . filesize(\$filename) . "\n";
   }
?>

Output Result

/PhpProject/php/phptest1.txt size 223
/PhpProject/php/phptest2.txt size 254
/PhpProject/php/phptest3.txt size 275
/PhpProject/php/phptest4.txt size 214
/PhpProject/php/phptest5.txt size 269
/PhpProject/php/phptest6.txt size 235
/PhpProject/php/phptest7.txt size 287
/PhpProject/php/phptest8.txt size 298
/PhpProject/php/phptest9.txt size 209
/PhpProject/php/phptest10.txt size 265

PHP Filesystem Reference Manual