WP_Filesystem_Base::getnumchmodfromh() WordPress Method

The WP_Filesystem_Base::getnumchmodfromh() function takes a numeric representation of a file's permissions and returns a string containing the file's permissions in octal notation.

WP_Filesystem_Base::getnumchmodfromh( string $mode ) #

Converts *nix-style file permissions to a octal number.


Description

Converts ‘-rw-r–r–‘ to 0644 From "info at rvgate dot nl"’s comment on the PHP documentation for chmod()


Top ↑

Parameters

$mode

(string)(Required)string The *nix-style file permissions.


Top ↑

Return

(string) Octal representation of permissions.


Top ↑

Source

File: wp-admin/includes/class-wp-filesystem-base.php

	public function getnumchmodfromh( $mode ) {
		$realmode = '';
		$legal    = array( '', 'w', 'r', 'x', '-' );
		$attarray = preg_split( '//', $mode );

		for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
			$key = array_search( $attarray[ $i ], $legal, true );

			if ( $key ) {
				$realmode .= $legal[ $key ];
			}
		}

		$mode  = str_pad( $realmode, 10, '-', STR_PAD_LEFT );
		$trans = array(
			'-' => '0',
			'r' => '4',
			'w' => '2',
			'x' => '1',
		);
		$mode  = strtr( $mode, $trans );

		$newmode  = $mode[0];
		$newmode .= $mode[1] + $mode[2] + $mode[3];
		$newmode .= $mode[4] + $mode[5] + $mode[6];
		$newmode .= $mode[7] + $mode[8] + $mode[9];

		return $newmode;
	}

Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.