list_files( string $folder = '', int $levels = 100, string[] $exclusions = array(), bool $include_hidden = false ): string[]|false

Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.

Description

The depth of the recursiveness can be controlled by the $levels param.

Parameters

$folderstringoptional
Full path to folder.

Default:''

$levelsintoptional
Levels of folders to follow, Default 100 (PHP Loop limit).

Default:100

$exclusionsstring[]optional
List of folders and files to skip.

Default:array()

$include_hiddenbooloptional
Whether to include details of hidden ("." prefixed) files.

Default:false

Return

string[]|false Array of files on success, false on failure.

Source

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

Changelog

VersionDescription
6.3.0Added the $include_hidden parameter.
4.9.0Added the $exclusions parameter.
2.6.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example for listing all files in the upload directory

    $upload_dir = wp_upload_dir();
    $folder = $upload_dir['basedir']
    $files = list_files( $folder, 2 );
    foreach ( $files as $file ) {
    	if ( is_file( $file ) ) {
    		$filesize = size_format( filesize( $file ) );
    		$filename = basename( $file ); 
    	}
    	echo esc_html( $filename . "-" . $filesize );
    }
  2. Skip to note 5 content

    This function returns a list of absolute paths (not file names!)

    print_r( list_files( __DIR__ ) );
    
    /*
    Output:
    
    Array (
        [0] => /Users/philipp/Sites/public/dev-site/wp-content/plugins/test/test.php
        [1] => /Users/philipp/Sites/public/dev-site/wp-content/plugins/test/readme.txt
        [2] => /Users/philipp/Sites/public/dev-site/wp-content/plugins/test/license.txt
    )
    */
  3. Skip to note 6 content

    Please note that this function seems to list only files but it is not.
    Some folder is empty, it’s showing in the list. Some folder is not empty, it’s showing only files.

    For example I have folders & files:
    wp-content/my/a
    wp-content/my/a/a.txt
    wp-content/my/b

    list_files() showing..
    wp-content/my/a/a.txt
    wp-content/my/b/

    Testing in WordPress 6.9-alpha-60636.

You must log in before being able to contribute a note or feedback.