PHP Get Files (By Extension, Prefix, Name, Date)

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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

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

1-file-extension.php
<?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

2-prefix-suffix.php
<?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

3-matching.php
<?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 contains SEARCH 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

4-date.php
<?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.

 

 

EXTRA BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

PHP Get & Filter Files List (click to enlarge)

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *