Showing posts with label scandir(). Show all posts
Showing posts with label scandir(). Show all posts

Wednesday 1 August 2012

PHP Glob() to Find Files


Using Glob() to Find Files

Many PHP functions have long and descriptive names. However it may be hard to tell what a function named glob() does unless you are already familiar with that term from elsewhere.
Think of it like a more capable version of the scandir() function. It can let you search for files by using patterns.

// get all php files
$files = glob('*.php');
print_r($files);

Output:

Array
(
    [0] => php1.php
    [1] => phptesting.php
    [2] => post_output.php
    [3] => test.php
)

To fetch multiple file types like this:

// get all php files AND txt files
$files = glob('*.{php,txt}', GLOB_BRACE);
print_r($files);

Output:

Array
(
    [0] => phptest.php
    [1] => function.php
    [2] => post_output.php
    [3] => test.php
    [4] => log.txt
    [5] => test.txt
)