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

You appear to be a bot. Output may be restricted

Description

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.

Usage

$string[]|false = list_files( $folder, $levels, $exclusions );

Parameters

$folder
( string ) optional – Optional. Full path to folder. Default empty.
$levels
( int ) optional default: 100 – Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
$exclusions
( string[] ) optional – Optional. List of folders and files to skip.

Returns

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

Source

File name: wordpress/wp-admin/includes/file.php
Lines:

1 to 44 of 44
function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
  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 ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
        continue;
      }

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

    closedir( $dir );
  }

  return $files;
}
 

 View on GitHub View on Trac