wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



list_files › WordPress Function

Seit2.6.0
Veraltetn/v
list_files ( $folder = '', $levels = 100, $exclusions = array(), $include_hidden = false )
Parameter: (4)
  • (string) $folder Optional. Full path to folder. Default empty.
    Erforderlich: Nein
    Standard: (leer)
  • (int) $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
    Erforderlich: Nein
    Standard: 100
  • (string[]) $exclusions Optional. List of folders and files to skip.
    Erforderlich: Nein
    Standard: array()
  • (bool) $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. Default false.
    Erforderlich: Nein
    Standard: false
Gibt zurück:
  • (string[]|false) Array of files on success, false on failure.
Definiert in:
Codex:
Changelog:
  • 4.9.0
  • 6.3.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.


Quellcode

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