wppaste
WordPress

WP_Filesystem_Direct::dirlist( string $path, bool $include_hidden = true, bool $recursive = false ): array|false

Since
2.5.0
Source
wp-admin/includes/class-wp-filesystem-direct.php:624
Gets details for files in a directory or a specific file.

Parameters

$pathstring
Path to directory or file.
$include_hiddenbooloptional
Whether to include details of hidden ("." prefixed) files.
Default true.Default: true
$recursivebooloptional
Whether to recursively include file details in nested directories.
Default false.Default: false

Return

array|false
Array of arrays containing file information. False if unable to list directory contents.
  • ...$0array

    Array of file information. Note that some elements may not be available on all filesystems.
    • $namestring

      Name of the file or directory.
    • $permsstring

      *nix representation of permissions.
    • $permsnstring

      Octal representation of permissions.
    • $numberfalse

      File number. Always false in this context.
    • $ownerstring|false

      Owner name or ID, or false if not available.
    • $groupstring|false

      File permissions group, or false if not available.
    • $sizeint|string|false

      Size of file in bytes. May be a numeric string. False if not available.
    • $lastmodunixint|string|false

      Last modified unix timestamp. May be a numeric string. False if not available.
    • $lastmodstring|false

      Last modified month (3 letters) and day (without leading 0), or false if not available.
    • $timestring|false

      Last modified time, or false if not available.
    • $typestring

      Type of resource. 'f' for file, 'd' for directory, 'l' for link.
    • $filesarray|false

      If a directory and $recursive is true, contains another array of files. False if unable to list directory contents.

Cost profile

Measured from the compiled opcodes, not from a stopwatch. Every number here is identical on any machine that runs the same PHP version, and every function in core is ranked by these figures.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
13–42

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 145.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Filesystem_Direct::dirlist() can have, taken from its control-flow graph on PHP 8.4.

WhenInstructionsCalls it makes
!->is_file() && !->is_dir()13->is_file()->is_dir()
!->is_file() && ->is_dir() && !->is_readable()17->is_file()->is_dir()->is_readable()
->is_file() && !->is_dir()19->is_file()basename()->is_dir()
!->is_file() && ->is_dir() && ->is_readable()22->is_file()->is_dir()->is_readable()dir()
->is_file() && ->is_dir() && !->is_readable()23->is_file()basename()->is_dir()->is_readable()
->is_file() && ->is_dir() && ->is_readable()28->is_file()basename()->is_dir()->is_readable()dir()
!->is_file() && ->is_dir() && ->is_readable() && $entry === false36->is_file()->is_dir()->is_readable()dir()trailingslashit()->read()->close()
->is_file() && ->is_dir() && ->is_readable() && $entry === false42->is_file()basename()->is_dir()->is_readable()dir()trailingslashit()->read()->close()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.414713–4414More instructions than PHP 8.4
8.114713–4414More instructions than PHP 8.4
8.214713–4414More instructions than PHP 8.4
8.314713–4414More instructions than PHP 8.4
8.414513–4214Compiles to the same instructions

Oldest PHP that compiles this body: 7.4. An instruction is not a fixed amount of time, so a version with the same count is not necessarily the same speed; what the count rules out is a difference in the work itself.

Uses · 11

Used by · 5

Source

	public function dirlist( $path, $include_hidden = true, $recursive = false ) {		if ( $this->is_file( $path ) ) {			$limit_file = basename( $path );			$path       = dirname( $path );		} else {			$limit_file = false;		} 		if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {			return false;		} 		$dir = dir( $path ); 		if ( ! $dir ) {			return false;		} 		$path = trailingslashit( $path );		$ret  = array(); 		while ( false !== ( $entry = $dir->read() ) ) {			$struc         = array();			$struc['name'] = $entry; 			if ( '.' === $struc['name'] || '..' === $struc['name'] ) {				continue;			} 			if ( ! $include_hidden && '.' === $struc['name'][0] ) {				continue;			} 			if ( $limit_file && $struc['name'] !== $limit_file ) {				continue;			} 			$struc['perms']       = $this->gethchmod( $path . $entry );			$struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );			$struc['number']      = false;			$struc['owner']       = $this->owner( $path . $entry );			$struc['group']       = $this->group( $path . $entry );			$struc['size']        = $this->size( $path . $entry );			$struc['lastmodunix'] = $this->mtime( $path . $entry );			$struc['lastmod']     = gmdate( 'M j', $struc['lastmodunix'] );			$struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );			$struc['type']        = $this->is_dir( $path . $entry ) ? 'd' : 'f'; 			if ( 'd' === $struc['type'] ) {				if ( $recursive ) {					$struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive );				} else {					$struc['files'] = array();				}			} 			$ret[ $struc['name'] ] = $struc;		} 		$dir->close();		unset( $dir ); 		return $ret;	}

History

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-admin/includes/class-wp-filesystem-direct.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.