list_files()WP 2.6.0

Get a list of files from the specified directory and from all nested directories (up to 100 levels of nesting).

The number of levels of nesting to be viewed can be specified in the second parameter $levels.

With the specified nesting limit, the function will get the name of the folder if it reaches the specified limit and there are still nested folders in the folder. Thus, in the final list of files, there will be files and folders, see example 2.

To use the function outside the admin area, you need to include the file:

require_once ABSPATH . 'wp-admin/includes/file.php';

No Hooks.

Returns

String[]|false. An array of file paths - when files have been retrieved. False - if files could not be retrieved.

Usage

$list = list_files( $folder = '', $levels = 100 );
$folder(string)
Path to the folder from which to get files.
Default: empty
$levels(number)

The level of folder nesting to view files. Default 100 (PHP limit).

For example, if 1 is specified here, only files from the specified folder will be collected. If 2, files from the specified folder and from folders within it will be collected. If 3, from the folder, its folders, and folders of its folders...
Default: 100

$exclusions(array) (since version 4.9)
List of folders and files to skip.
Default: array()
$include_hidden(true/false) (since version 6.3)
Whether to include hidden files in the list - files starting with a dot ., for example .git, .htaccess.
Default: false

Examples

0

#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
)
*/
0

#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.

0

#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.

list_files() code WP 7.0

function list_files( $folder = '', $levels = 100, $exclusions = array(), $include_hidden = false ) {
	if ( empty( $folder ) ) {
		return false;
	}

	$folder = trailingslashit( $folder );

	if ( ! $levels ) {
		return false;
	}

	$files = array();

	$dir = @opendir( $folder );

	if ( $dir ) {
		while ( ( $file = readdir( $dir ) ) !== false ) {
			// Skip current and parent folder links.
			if ( in_array( $file, array( '.', '..' ), true ) ) {
				continue;
			}

			// Skip hidden and excluded files.
			if ( ( ! $include_hidden && '.' === $file[0] ) || in_array( $file, $exclusions, true ) ) {
				continue;
			}

			if ( is_dir( $folder . $file ) ) {
				$files2 = list_files( $folder . $file, $levels - 1, array(), $include_hidden );
				if ( $files2 ) {
					$files = array_merge( $files, $files2 );
				} else {
					$files[] = $folder . $file . '/';
				}
			} else {
				$files[] = $folder . $file;
			}
		}

		closedir( $dir );
	}

	return $files;
}