wppaste
WordPress

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

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

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$pathstringoptional
Path to directory or file.Default: '.'
$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 value

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.
    • $numberint|string|false

      File number. May be a numeric string. False if not available.
    • $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.

Performance profile

How much work a call to WP_Filesystem_ftpsockets::dirlist() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

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
24–39

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 92.

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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
!->is_file() && empty($list) && !->exists()24->is_file(), mbstring_binary_safe_encoding(), ->dirlist(), ->exists(), reset_mbstring_encoding()
!->is_file() && !empty($list)27–28->is_file(), mbstring_binary_safe_encoding(), ->dirlist(), trailingslashit(), reset_mbstring_encoding()
!->is_file() && empty($list) && ->exists()31–32->is_file(), mbstring_binary_safe_encoding(), ->dirlist(), ->exists(), trailingslashit(), reset_mbstring_encoding()
->is_file() && empty($list) && !->exists()31->is_file(), basename(), mbstring_binary_safe_encoding(), ->dirlist(), ->exists(), reset_mbstring_encoding()
->is_file() && !empty($list)34–35->is_file(), basename(), mbstring_binary_safe_encoding(), ->dirlist(), trailingslashit(), reset_mbstring_encoding()
->is_file() && empty($list) && ->exists()38–39->is_file(), basename(), mbstring_binary_safe_encoding(), ->dirlist(), ->exists(), trailingslashit(), reset_mbstring_encoding()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9224–3914
8.59224–3914
8.49224–39145 fewer instructions than PHP 8.3
8.39724–4114
8.29724–4114
8.19724–4114
7.49724–4114

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 5

Source code

	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;		} 		mbstring_binary_safe_encoding(); 		$list = $this->ftp->dirlist( $path ); 		if ( empty( $list ) && ! $this->exists( $path ) ) { 			reset_mbstring_encoding(); 			return false;		} 		$path = trailingslashit( $path );		$ret  = array(); 		foreach ( $list as $struc ) { 			if ( '.' === $struc['name'] || '..' === $struc['name'] ) {				continue;			} 			if ( ! $include_hidden && '.' === $struc['name'][0] ) {				continue;			} 			if ( $limit_file && $struc['name'] !== $limit_file ) {				continue;			} 			if ( 'd' === $struc['type'] ) {				if ( $recursive ) {					$struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive );				} else {					$struc['files'] = array();				}			} 			// Replace symlinks formatted as "source -> target" with just the source name.			if ( $struc['islink'] ) {				$struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );			} 			// Add the octal representation of the file permissions.			$struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); 			$ret[ $struc['name'] ] = $struc;		} 		reset_mbstring_encoding(); 		return $ret;	}

Changelog

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.8.8 tag, from src/wp-admin/includes/class-wp-filesystem-ftpsockets.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.