2010-01-09 144 views
3

我一直在研究运行在文件系统上的WordPress源代码,当我碰到这几行时,我真的不确定它们在做什么?stat()和&符号运算符

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

+1一个有效的问题,考虑到你没有看到很多利用位运算符中PHP。有关按位运算符的更多信息,请查看http://php.net/manual/en/language.operators.bitwise.php – 2010-01-09 21:26:23

回答

3

该代码使用按位操作,以确保文件的权限是不高于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); 
+1

“,以确保在这种情况下特定文件属性被设置为” - “或”未设置“:除去666(rw-rw-rw-)中未包含的所有内容 - 实际上这意味着执行位。 – Wim 2010-01-09 20:59:42

+0

非常感谢乔恩!但是我猜如果文件权限已经是600,那么$ perms会保持600? – TheDeadMedic 2010-01-09 21:02:02

+0

@TheDeadMedic - 正确。该代码只会删除权限,不会添加它们。 – 2010-01-10 00:41:52

1

它更改允许在目录中写入的权限..我想。检查出stat()chmod()

+0

是的,但为什么首先要获取chmod信息,以及为什么'&&它与666?我也不明白。让我们看看是否有人可以解决这个谜题。 – 2010-01-09 20:53:29

+0

它会删除额外的位并只保留'rwxrwxrwx'部分,无论哪个位被设置。 – Blindy 2010-01-09 20:56:45

0

0666是unix rwxrwxrwx权限的八进制表示法,所以我假设$stat['mode']返回文件夹的权限。然后,他们与0666掩码进行按位AND操作,以检查您是否有至少对自己,组和其他人的读/写/执行权限。

+3

'x'权限为0(十六进制/八进制中的1),这些未设置。所以666的意思是rw-rw-rw-。另外,AND运算符只保留*为*两个*操作数中的1,因此代码实际上*删除了*不可读写的所有权限。 – Wim 2010-01-09 20:58:20