list_files()WP 2.6.0

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

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 6.6.2

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;
}