1

below code only list files in directories and does not search sub-directories:

<?php
//path to directory to scan. i have included a wildcard for a sub directory
$directory = "/var/www/*/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 1000);

//display images
foreach ($imgs as $img) {
    echo $img . '<br>';
}
?>

but How can list all specific extension (for example *.jpg) in all directories and

sub directories of a path by using PHP?

Ali
  • 602
  • 6
  • 18

1 Answers1

2

You should use a recursive function. This can help you: List all the files and folders in a Directory with PHP recursive function Look at the answer and change it to your own needs. (wanted to help you using your own code but it's too far from the right way doing it).

Tzahi Serruya
  • 147
  • 2
  • 11