Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for specific file extensions in a folder/directory (PHP)

I'm trying to design a program in PHP that would allow me to find files with specific file extensions (example .jpg, .shp etc) in a known directory which consists of multiple folders. Sample code, documentation or information about what methods I will be required to use will be much appreciated.

like image 935
Jason Avatar asked Dec 02 '22 06:12

Jason


2 Answers

glob is pretty easy:

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

There are a few suggestions for recursive descent at the readdir page.

like image 83
Ewan Todd Avatar answered Dec 03 '22 23:12

Ewan Todd


Take a look at PHP's SPL DirectoryIterator.

like image 25
dnagirl Avatar answered Dec 03 '22 23:12

dnagirl