Php stat()和安培运算符

Php stat()和安培运算符,php,Php,我一直在研究在文件系统上运行的WordPress源代码,当我遇到这几行代码时,我真的不太确定它们是做什么的 $stat = stat( dirname( $new_file )); $perms = $stat['mode'] & 0000666; @ chmod( $new_file, $perms ); 它更改了允许在目录中写入的权限。。我想。签出并执行操作。0666是unixrwxrwx权限的八进制符号,因此我假设$stat['mode']返回文件夹的权限。然后用0666掩码将它

我一直在研究在文件系统上运行的WordPress源代码,当我遇到这几行代码时,我真的不太确定它们是做什么的

$stat = stat( dirname( $new_file ));
$perms = $stat['mode'] & 0000666;
@ chmod( $new_file, $perms );

它更改了允许在目录中写入的权限。。我想。签出并执行操作。

0666
是unix
rwxrwx
权限的八进制符号,因此我假设
$stat['mode']
返回文件夹的权限。然后用
0666
掩码将它们按位编辑,以检查您是否至少对self、group和其他文件具有读/写/执行权限。

该代码使用按位操作来确保文件的权限不高于666。 要分解它:

// Retrieves the file details, including current file permissions.
$stat = stat( dirname( $new_file )); 

// The file permissions are and-ed with the octal value 0000666 to make
// sure that the file mode is no higher than 666. In other words, it locks
// the file down, making sure that current permissions are no higher than 666,
// or owner, group and world read/write.
$perms = $stat['mode'] & 0000666; 

// Finally, the new permissions are set back on the file
@chmod( $new_file, $perms );

是的,但是为什么首先要获取chmod信息,为什么要使用666获取chmod信息?我也不明白。让我们看看是否有人能解答这个谜题。它会删除多余的位,只保留
rwxrwx
部分,不管设置了哪个位。
x
权限是位0(十六进制/八进制中的1),这些都没有设置。所以666意味着rw-rw-rw-。而且,AND运算符只保留两个操作数中都为1的位,因此代码实际上会删除所有未读/写的权限。“为了确保设置了特定的文件属性”——或者在这种情况下为“未设置”:666(rw rw rw-)中未包含的所有内容都将被删除——实际上这意味着执行位。非常感谢Jon!但是我猜,如果文件的perms已经是,例如,600,那么$perms将仍然是600?@TheDeadMedic-正确。代码只删除权限,不添加权限。+1对于一个有效的问题,考虑到在PHP中使用位运算符的情况并不多。有关按位运算符的更多信息,请参阅