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

I am trying to create a program in PHP that will allow me to find files with specific file extensions (example .jpg, .shp, etc.) in a well-known directory that consists of several folders. Sample code, documentation, or information about which methods I need will be appreciated.

+4
source share
5 answers

glob pretty easy:

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

There are several suggestions for recursive descent on the readdir page.

+11
source

Take a look at the PHP SPL DirectoryIterator .

+6
source

I believe the PHP glob () function is exactly what you are looking for:

http://php.net/manual/en/function.glob.php

+2
source

Use readdir to get a list of files, and fnmatch to determine if it matches your file name pattern. Do it all inside the function and call your function when you find the directories. Ask another question if you get stuck in implementing this (or a comment if you really don't know where to start).

0
source

glob will provide you with all the files in this directory, but not subdirectories. If you also need this, you need: 10. get recursive, 20. goto 10.

Here's the pseudo pseudo code:

 function getFiles($pattern, $dir) { $files = glob($dir . $pattern); $folders = glob($dir, GLOB_ONLYDIR); foreach ($folders as $folder) { $files = $files + getFiles($folder); } return $files; } 

The foregoing, obviously, will need to be set up to make it work, but I hope you get this idea (remember that you should not refer to the catalog links to ".." or ".", Or you will be in the endless city of the loop).

0
source

All Articles