Created
December 5, 2021 14:22
-
-
Save ilyahoilik/55b470436971b90ad0fb0c699ce3487f to your computer and use it in GitHub Desktop.
This is how to list all files inside a directory and all subdirectories. Works well in browser and command line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$is_cli = php_sapi_name() == 'cli'; | |
/** | |
* List all files inside a directory and all subdirectories. | |
* | |
* @param mixed $directory | |
* @param bool $is_cli | |
* @return void | |
*/ | |
function list_files($directory, $is_cli) | |
{ | |
$iterator = new DirectoryIterator($directory); | |
foreach ($iterator as $item) { | |
if (! $item->isDot()) { | |
if ($item->isDir()) { | |
list_files($directory . '/' . $item, $is_cli); | |
} else { | |
echo $directory . '/' . $item->getFilename() . ($is_cli ? "\n" : '<br>'); | |
} | |
} | |
} | |
} | |
list_files(__DIR__, $is_cli); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment