WP_Filesystem_Base::getnumchmodfromh()publicWP 2.5.0

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

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

Method of the class: WP_Filesystem_Base{}

No Hooks.

Return

String. Octal representation of permissions.

Usage

$WP_Filesystem_Base = new WP_Filesystem_Base();
$WP_Filesystem_Base->getnumchmodfromh( $mode );
$mode(string) (required)
string The *nix-style file permissions.

Changelog

Since 2.5.0 Introduced.

WP_Filesystem_Base::getnumchmodfromh() code WP 6.5.2

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