list_files()
Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep. The depth of the recursiveness can be controlled by the $levels param.
The depth of the recursiveness can be controlled by the $levels param.
No Hooks.
Return
String[]|false
. Array of files on success, false on failure.
Usage
list_files( $folder, $levels, $exclusions, $include_hidden );
- $folder(string)
- Full path to folder.
Default: '' - $levels(int)
- Levels of folders to follow, Default 100 (PHP Loop limit).
Default: 100 - $exclusions(string[])
- List of folders and files to skip.
Default: array() - $include_hidden(true|false)
- Whether to include details of hidden ("." prefixed) files.
Default: false
Examples
#1 Example of reading files from a directory
For an example of how the function works, let's get all the WordPress files.
//require_once ABSPATH . 'wp-admin/includes/file.php'; $files = list_files( ABSPATH ); print_r( $files ); /* Let's get Array ( [0] => /home/t/public_html/xmlrpc.php [1] => /home/t/public_html/wp-mail.php [2] => /home/t/public_html/wp-includes/ms-settings.php ... [991] => /home/t/public_html/wp-admin/css/colors/sunrise/colors.css ) */
#2 Example of retrieving files with the restriction of viewing subfolders
//require_once ABSPATH . 'wp-admin/includes/file.php'; $files = list_files( ABSPATH, 1 ); print_r( $files ); /* Let's get Array ( [0] => /home/t/public_html/xmlrpc.php [1] => /home/t/public_html/wp-mail.php [2] => /home/t/public_html/wp-includes/ [3] => /home/t/public_html/wp-settings.php [4] => /home/t/public_html/wp-config-sample.php [5] => /home/t/public_html/wp-comments-post.php [6] => /home/t/public_html/wp-admin/ [7] => /home/t/public_html/wp-blog-header.php [8] => /home/t/public_html/wp-links-opml.php [9] => /home/t/public_html/index.php [10] => /home/t/public_html/wp-login.php [11] => /home/t/public_html/wp-trackback.php [12] => /home/t/public_html/wp-activate.php [13] => /home/t/public_html/wp-load.php [14] => /home/t/public_html/wp-signup.php [15] => /home/t/public_html/wp-cron.php ) */
Note that in addition to the files, the names of the subfolders were also retrieved.
#3 Example for listing all files in the upload directory
require_once ABSPATH . 'wp-admin/includes/file.php'; $folder = wp_upload_dir()['basedir']; $files = list_files( $folder, 3 ); foreach ( $files as $file ) { if ( is_file( $file ) ) { $filesize = size_format( filesize( $file ) ); echo esc_html( "$file - $filesize" ) . "\n"; } } /* Result: /home/wp-kama.com/www/wp-content/uploads/2022/03/clipboard-image-886236.png - 4 KB /home/wp-kama.com/www/wp-content/uploads/2022/03/clipboard-image-99186-120x120.png - 2 KB /home/wp-kama.com/www/wp-content/uploads/2022/03/Variables-Blacklist.webp - 12 KB /home/wp-kama.com/www/wp-content/uploads/2022/03/clipboard-image-886236-120x50.png - 1 KB /home/wp-kama.com/www/wp-content/uploads/2022/03/links-bookmarks-in-WordPress-120x120.webp - 1 KB ... */
Changelog
Since 2.6.0 | Introduced. |
Since 4.9.0 | Added the $exclusions parameter. |
Since 6.3.0 | Added the $include_hidden parameter. |