PHP - Fonction glob ()

La fonction glob () peut renvoyer un tableau contenant des noms de fichiers ou des répertoires correspondant au modèle spécifié. Cette fonction peut renvoyer un tableau contenant les fichiers / répertoires correspondants ou false en cas d'échec.

Syntaxe

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

La fonction glob () peut rechercher tous les pathnames correspondant aux modèles selon les règles utilisées par la fonction glob (), qui est similaire aux règles utilisées par les shells courants.

Exemple 1

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

Production

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
)

Exemple-2

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

Production

/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