Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active June 27, 2023 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/b1c723d7f3148a7b2b97d0687fa2c52b to your computer and use it in GitHub Desktop.
Save code-boxx/b1c723d7f3148a7b2b97d0687fa2c52b to your computer and use it in GitHub Desktop.
PHP List Files & Folders

WAYS TO LIST FILES & FOLDERS IN PHP

https://code-boxx.com/list-files-folders-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) GET FILES/FOLDERS
$dir = "YOUR/FOLDER/";
$all = array_diff(scandir($dir), [".", ".."]);
$eol = PHP_EOL;
// (B) LOOP THROUGH ALL
foreach ($all as $ff) {
if (is_file($dir . $ff)) { echo "{$ff} - file {$eol}"; }
if (is_dir($dir . $ff)) { echo "{$ff} - folder {$eol}"; }
}
<?php
// (A) RECURSIVE SCAN
function rscan ($dir) {
$all = array_diff(scandir($dir), [".", ".."]);
$eol = PHP_EOL;
foreach ($all as $ff) {
if (is_file($dir . $ff)) { echo "{$ff} - file {$eol}"; }
if (is_dir($dir . $ff)) {
echo "{$ff} - folder {$eol}";
rscan("$dir$ff/");
}
}
}
// (B) GO!
rscan("YOUR/FOLDER/");
<?php
// (A) GET FILES/FOLDERS
$all = glob("YOUR/FOLDER/*");
// $all = glob("YOUR/FOLDER/*.{jpg,jpeg,webp,png,gif}", GLOB_BRACE);
$eol = PHP_EOL;
// (B) LOOP THROUGH ALL
foreach ($all as $ff) {
if (is_file($ff)) { echo "{$ff} - file {$eol}"; }
if (is_dir($ff)) { echo "{$ff} - folder {$eol}"; }
}
<?php
// (A) RECURSIVE GLOB
function rglob ($dir, $ext="*") {
// (A1) GET FILES
$eol = PHP_EOL;
foreach (glob("$dir$ext", GLOB_BRACE) as $ff) {
if (is_file($ff)) { echo "{$ff} - file {$eol}"; }
}
// (A2) GET FOLDERS
foreach (glob("$dir*", GLOB_ONLYDIR | GLOB_MARK) as $ff) {
echo "{$ff} - folder {$eol}";
rglob($ff, $ext);
}
}
// (B) GO!
rglob("YOUR/FOLDER/");
// rglob("YOUR/FOLDER/", "*.{jpg,jpeg,webp,png,gif}");
<?php
// (A) OPEN DIR
$dir = "YOUR/FOLDER/";
$dh = opendir($dir) or exit("Error opening $dir");
$eol = PHP_EOL;
// (B) READ DIR
while ($ff = readdir($dh)) { if ($ff!="." && $ff!="..") {
$ff = $dir . $ff;
if (is_file($ff)) { echo "{$ff} - file {$eol}"; }
if (is_dir($ff)) { echo "{$ff} - folder {$eol}"; }
}}
<?php
// (A) RECURSIVE OPEN-READ
function rread ($dir) {
$dh = opendir($dir);
$eol = PHP_EOL;
while ($ff = readdir($dh)) { if ($ff!="." && $ff!="..") {
$ff = $dir . $ff;
if (is_file($ff)) { echo "{$ff} - file {$eol}"; }
if (is_dir($ff)) {
echo "{$ff} - folder {$eol}";
rread("$ff/");
}
}}
}
// (B) GO!
rread("YOUR/FOLDER/");
<?php
// (A) NEW DIRECTORY ITERATOR
$iterator = new DirectoryIterator("YOUR/FOLDER/");
$eol = PHP_EOL;
// (B) LOOP FOLDER
foreach ($iterator as $ff) {
if ($ff->isDot()) { continue; }
if ($ff->isFile()) { echo "{$ff->getFilename()} - file {$eol}"; }
if ($ff->isDir()) { echo "{$ff->getFilename()} - folder {$eol}"; }
}
<?php
// (A) RECURSIVE ITERATOR
function riterate ($dir) {
$iterator = new DirectoryIterator($dir);
$eol = PHP_EOL;
foreach ($iterator as $ff) {
if ($ff->isDot()) { continue; }
if ($ff->isFile()) { echo "{$ff->getFilename()} - file {$eol}"; }
if ($ff->isDir()) {
echo "{$ff->getFilename()} - folder {$eol}";
riterate("{$dir}{$ff->getFilename()}/");
}
}
}
// (B) GO!
riterate("YOUR/FOLDER/");
<?php
// (A) GLOB ITERATOR
$dir = "D:/DOCS/";
$eol = PHP_EOL;
// $iterator = new ArrayObject(glob("$dir*.{jpg,jpeg,gif,png,bmp,webp}", GLOB_BRACE));
$iterator = new ArrayObject(glob("$dir*", GLOB_BRACE));
$iterator = $iterator->getIterator();
/* (B) OPTIONAL - PAGINATION
$pgNow = 0; // current page
$pgPer = 10; // entries per page
$iterator = new LimitIterator($iterator, $pgNow * $pgPer, $pgPer);
*/
// (C) LOOP
foreach ($iterator as $ff) {
if (is_file($ff)) { echo "{$ff} - file {$eol}"; }
if (is_dir($ff)) { echo "{$ff} - folder {$eol}"; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment