PHP

HOW TO LIST FILES & FOLDERS IN PHP

(quick guide & examples)

GET ALL FILES & FOLDERS $all = array_diff(scandir("YOUR/FOLDER/"), [".", ".."]);

SCAN DIRECTORY

01

LOOP THROUGH FILES & FOLDERS foreach ($all as $ff) { ... }

GET IMAGE FILES ONLY $all = glob("YOUR/FOLDER/*.{jpg,jpeg,webp,png,gif}", GLOB_BRACE);

GLOB PATTERN

02

LOOP THROUGH FILES & FOLDERS foreach ($all as $ff) { ... }

OPEN DIR $dir = "YOUR/FOLDER/"; $dh = opendir($dir);

OPEN & READ DIRECTORY

03

READ DIR while ($ff = readdir($dh)) {   if ($ff!="." && $ff!="..") { ... } }

ITERATOR OBJECT $iterator = new DirectoryIterator("YOUR/FOLDER/");

DIRECTORY ITERATOR

04

LOOP THROUGH FILES & FOLDERS foreach ($iterator as $ff) { ... }