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

In this article

Gets details for files in a directory or a specific file.

Parameters

$pathstringrequired
Path to directory or file.
$include_hiddenbooloptional
Whether to include details of hidden ("." prefixed) files.

Default:true

$recursivebooloptional
Whether to recursively include file details in nested directories.

Default:false

Return

array|false Array of arrays containing file information. False if unable to list directory contents.
  • 0... array
    Array of file information. Note that some elements may not be available on all filesystems.
    • name string
      Name of the file or directory.
    • perms string
      *nix representation of permissions.
    • permsn string
      Octal representation of permissions.
    • number false
      File number. Always false in this context.
    • owner string|false
      Owner name or ID, or false if not available.
    • group string|false
      File permissions group, or false if not available.
    • size int|string|false
      Size of file in bytes. May be a numeric string.
      False if not available.
    • lastmodunix int|string|false
      Last modified unix timestamp. May be a numeric string.
      False if not available.
    • lastmod string|false
      Last modified month (3 letters) and day (without leading 0), or false if not available.
    • time string|false
      Last modified time, or false if not available.
    • type string
      Type of resource. 'f' for file, 'd' for directory, 'l' for link.
    • files array|false
      If a directory and $recursive is true, contains another array of files. False if unable to list directory contents.

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

Changelog

VersionDescription
2.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.