• PHP Video Tutorials

PHP - Function glob()



The glob() function can return an array containing file names or directories matching the specified pattern. This function can return an array containing matching files/directories or false on failure.

Syntax

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

The glob() function can search for all pathnames matching patterns according to the rules used by glob() function, which is similar to rules used by common shells.

Example-1

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

Output

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
)

Example-2

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

Output

/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_function_reference.htm
Advertisements