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
Parameters
$pathstring- Path to directory or file.
$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.
...$0arrayArray of file information. Note that some elements may not be available on all filesystems.$namestringName of the file or directory.$permsstring*nix representation of permissions.$permsnstringOctal representation of permissions.$numberfalseFile number. Always false in this context.$ownerstring|falseOwner name or ID, or false if not available.$groupstring|falseFile permissions group, or false if not available.$sizeint|string|falseSize of file in bytes. May be a numeric string. False if not available.$lastmodunixint|string|falseLast modified unix timestamp. May be a numeric string. False if not available.$lastmodstring|falseLast modified month (3 letters) and day (without leading 0), or false if not available.$timestring|falseLast modified time, or false if not available.$typestringType of resource. 'f' for file, 'd' for directory, 'l' for link.$filesarray|falseIf a directory and$recursiveis 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
- Scaling
- Scales with input
- Instructions
- 13–42
- Plugin surface
- None
- Called by
- 5
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 145.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls 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 === false | 36 | ->is_file()->is_dir()->is_readable()dir()trailingslashit()->read()->close() |
->is_file() && ->is_dir() && ->is_readable() && $entry === false | 42 | ->is_file()basename()->is_dir()->is_readable()dir()trailingslashit()->read()->close() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
7.4 | 147 | 13–44 | 14 | More instructions than PHP 8.4 |
8.1 | 147 | 13–44 | 14 | More instructions than PHP 8.4 |
8.2 | 147 | 13–44 | 14 | More instructions than PHP 8.4 |
8.3 | 147 | 13–44 | 14 | More instructions than PHP 8.4 |
8.4 | 145 | 13–42 | 14 | Compiles 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
- trailingslashit()Appends a trailing slash.
- WP_Filesystem_Direct::is_file()Checks if resource is a file.
- WP_Filesystem_Direct::is_dir()Checks if resource is a directory.
- WP_Filesystem_Direct::is_readable()Checks if a file is readable.
- WP_Filesystem_Direct::gethchmod()
- WP_Filesystem_Direct::getnumchmodfromh()
- WP_Filesystem_Direct::owner()Gets the file owner.
- WP_Filesystem_Direct::group()Gets the file's group.
- WP_Filesystem_Direct::size()Gets the file size (in bytes).
- WP_Filesystem_Direct::mtime()Gets the file modification time.
- WP_Filesystem_Direct::dirlist()Gets details for files in a directory or a specific file.
Used by · 5
- WP_Filesystem_Direct::chgrp()Changes the file group.
- WP_Filesystem_Direct::chmod()Changes filesystem permissions.
- WP_Filesystem_Direct::chown()Changes the owner of a file or directory.
- WP_Filesystem_Direct::delete()Deletes a file or directory.
- WP_Filesystem_Direct::dirlist()Gets details for files in a directory or a specific file.
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.
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.