WP_Filesystem_FTPext::parselisting( string $line ): array

In this article

Parameters

$linestringrequired

Return

array Array of file information.
  • name string
    Name of the file or directory.
  • perms string
    *nix representation of permissions.
  • permsn string
    Octal representation of permissions.
  • number string|false
    File number as a string, or false if not available.
  • owner string|false
    Owner name or ID, or false if not available.
  • group string|false
    File permissions group, or false if not available.
  • size string|false
    Size of file in bytes as a string, or false if not available.
  • lastmodunix string|false
    Last modified unix timestamp as a string, or 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 parselisting( $line ) {
	static $is_windows = null;

	if ( is_null( $is_windows ) ) {
		$is_windows = stripos( ftp_systype( $this->link ), 'win' ) !== false;
	}

	if ( $is_windows && preg_match( '/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer ) ) {
		$b = array();

		if ( $lucifer[3] < 70 ) {
			$lucifer[3] += 2000;
		} else {
			$lucifer[3] += 1900; // 4-digit year fix.
		}

		$b['isdir'] = ( '<DIR>' === $lucifer[7] );

		if ( $b['isdir'] ) {
			$b['type'] = 'd';
		} else {
			$b['type'] = 'f';
		}

		$b['size']   = $lucifer[7];
		$b['month']  = $lucifer[1];
		$b['day']    = $lucifer[2];
		$b['year']   = $lucifer[3];
		$b['hour']   = $lucifer[4];
		$b['minute'] = $lucifer[5];
		$b['time']   = mktime( $lucifer[4] + ( strcasecmp( $lucifer[6], 'PM' ) === 0 ? 12 : 0 ), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3] );
		$b['am/pm']  = $lucifer[6];
		$b['name']   = $lucifer[8];
	} elseif ( ! $is_windows ) {
		$lucifer = preg_split( '/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY );

		if ( $lucifer ) {
			// echo $line."\n";
			$lcount = count( $lucifer );

			if ( $lcount < 8 ) {
				return '';
			}

			$b           = array();
			$b['isdir']  = 'd' === $lucifer[0][0];
			$b['islink'] = 'l' === $lucifer[0][0];

			if ( $b['isdir'] ) {
				$b['type'] = 'd';
			} elseif ( $b['islink'] ) {
				$b['type'] = 'l';
			} else {
				$b['type'] = 'f';
			}

			$b['perms']  = $lucifer[0];
			$b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
			$b['number'] = $lucifer[1];
			$b['owner']  = $lucifer[2];
			$b['group']  = $lucifer[3];
			$b['size']   = $lucifer[4];

			if ( 8 === $lcount ) {
				sscanf( $lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day'] );
				sscanf( $lucifer[6], '%d:%d', $b['hour'], $b['minute'] );

				$b['time'] = mktime( $b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year'] );
				$b['name'] = $lucifer[7];
			} else {
				$b['month'] = $lucifer[5];
				$b['day']   = $lucifer[6];

				if ( preg_match( '/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2 ) ) {
					$b['year']   = gmdate( 'Y' );
					$b['hour']   = $l2[1];
					$b['minute'] = $l2[2];
				} else {
					$b['year']   = $lucifer[7];
					$b['hour']   = 0;
					$b['minute'] = 0;
				}

				$b['time'] = strtotime( sprintf( '%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute'] ) );
				$b['name'] = $lucifer[8];
			}
		}
	}

	// Replace symlinks formatted as "source -> target" with just the source name.
	if ( isset( $b['islink'] ) && $b['islink'] ) {
		$b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
	}

	return $b;
}

User Contributed Notes

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