So you have a project that needs to filter out a list of files from a folder – The glob()
function is probably your best bet, but it can be pretty intimidating for some beginners. Fear not, here are some of the common “search patterns” that I gathered from the Internet. Read on!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
PHP GLOB SEARCH/FILTER FILES
All right, let us now get the common examples of filtering a list of files using PHP glob.
1) GET BY FILE EXTENSION
<?php
// (A) TARGET FOLDER TO LIST
$dir = __DIR__ . DIRECTORY_SEPARATOR . "demo" . DIRECTORY_SEPARATOR;
// (B) GET ALL JPG FILES
$images = glob($dir . "*.jpg");
print_r($images);
// (C) GET ALL IMAGE FILES
$images = glob($dir . "*.{jpg,png,gif,webp,bmp}", GLOB_BRACE);
print_r($images);
// (D) GET ALL DOCUMENTS
$docs = glob($dir . "*.{txt,xlsx,docx,odt}", GLOB_BRACE);
print_r($docs);
- To get a specific type of file –
glob("PATH/*.EXT")
. - If you want to get a list of file extensions, use
GLOB_BRACE
to help you –glob("PATH/*.{EXT, EXT, ...}", GLOB_BRACE)
.
2) GET BY FILE PREFIX & SUFFIX
<?php
// (A) TARGET FOLDER TO LIST
$dir = __DIR__ . DIRECTORY_SEPARATOR . "demo" . DIRECTORY_SEPARATOR;
// (B) GET ALL FILES STARTING WITH "I_"
$prefix = glob($dir . "i_*");
print_r($prefix);
// (C) GET ALL FILES STARTING WITH "I_" OR "J_"
$prefix = glob($dir . "{i_,j_}*", GLOB_BRACE);
print_r($prefix);
// (D) GET ALL FILES ENDING WITH "_J"
$suffix = glob($dir . "*_j.*");
print_r($suffix);
// (E) GET ALL FILES STARTING WITH I_ AND ENDING WITH "_J"
$presuf = glob($dir . "i_*_j.*");
print_r($presuf);
- To get files starting with a certain prefix –
glob("PATH/PREFIX*")
. - If you have multiple prefixes, use
GLOB_BRACE
once again to help you –glob("PATH/{PREFIX-A, PREFIX-B, ...}", GLOB_BRACE)
. - To get files ending with a certain suffix –
glob("PATH/*SUFFIX.*")
. - To get files starting with a certain prefix and ending with a certain suffix –
glob("PATH/PREFIX*SUFFIX.*")
.
P.S. You can further filter by the file extension if you want. For example, glob("PATH/PREFIX*.{jpg,png,gif}", GLOB_BRACE)
.
3) GET BY FILE NAME
<?php
// (A) TARGET FOLDER TO LIST
$dir = __DIR__ . DIRECTORY_SEPARATOR . "demo" . DIRECTORY_SEPARATOR;
// (B) CASE SENSITIVE - MUST CONTAIN "APP"
$search = glob($dir . "*app*");
print_r($search);
// (C) CASE INSENSITIVE - MUST CONTAIN "APP"
$search = glob($dir . "*[aA][pP][pP]*");
print_r($search);
// (D) CASE INSENSITIVE - MUST CONTAIN "APP" OR "OPP"
$search = glob($dir . "*[aAoO][pP][pP]*");
print_r($search);
- To search by the file name (case sensitive) –
glob("FOLDER/*SEARCH*")
. I.E. It’s a match as long as the file name containsSEARCH
anywhere. - Case-sensitive search is kind of painful, you will have to manually use square brackets to match each character –
glob([aA][bB][cC][...])
4) GET FILES BY DATE
<?php
// (A) TARGET FOLDER TO LIST
$dir = __DIR__ . DIRECTORY_SEPARATOR . "demo" . DIRECTORY_SEPARATOR;
// (B) ALL FILES OLDER THAN 7 DAYS
// 7 DAYS = 604800 SECONDS
$old = strtotime("now") - 604800;
foreach (glob($dir . "*.*") as $file) {
if (filemtime($file) <= $old) { echo "$file"; }
}
// (C) GET FILES CREATED ON EXACT DATE
$start = strtotime("2012-12-12 00:00:00");
$end = strtotime("2012-12-12 23:59:59");
foreach (glob($dir . "*.*") as $file) {
$filetime = filemtime($file);
if ($filetime>=$start && $filetime<=$end) { echo "$file"; }
}
Sadly, glob()
cannot filter by the file date “natively”. We will have to manually loop through all the files and check the timestamp individually.
EXTRAS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
A SMALL NOTE ON PERFORMANCE & READING SUB-FOLDERS
Take note of how glob()
works, it returns an array. It’s all nice and cool but runs into memory issues when you deal with massive folders. There is a way around it by using an iterator, check out my other “ways to list files folders” tutorial below – Which also covers “how to read sub-folders”.
LINKS & REFERENCES
- Ways To List Files & Folders In PHP – Code Boxx
- Glob – Official PHP Manual
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!