Luke Burns
Luke Burns

Reputation: 1929

Iterate over specific files in a directory

I need to get all images that begin with "t_" using glob. What pattern should I use to do this?

        //get any image files that begin with "t_" -- (t_image.jpg) not (image.jpg)
        $images = glob("" . $dir . "*.jpg");

        foreach($images as $image)
        {
            echo $image;
        }

Upvotes: 1

Views: 875

Answers (1)

Dave Jarvis
Dave Jarvis

Reputation: 31171

foreach (glob("t_*.jpg") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}

This implies:

foreach (glob("$dir/t_*.jpg") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}

See also:

https://www.php.net/manual/en/function.glob.php

Upvotes: 4

Related Questions