Reputation: 6750
Found this bit of code in FrogCms
function isWritable($file=null) {
$perms = fileperms($file);
if (is_writable($file) || ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002))
return true;
}
I had a hard time understanding this part
(is_writable($file) || ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002))
After doing some research i know that 0x0080
is permission for owner, 0x0010
is permission for group and 0x0002
is permission for other.Where do this value come form? Is this is a predetermined value for permission system?
And can someone explain to me for example how $perms & 0x0080
resolve to true, cause for example fileperms(__FILE__)
return a value like 33206.How can u
compare 33206 with 0x0080?
Upvotes: 1
Views: 2316
Reputation: 437424
The constants you refer to (0x0080
etc) do not have the exact meanings you think they do. See the fileperms
documentation:
0x0080 = owner writable
0x0010 = group writable
0x0002 = world writable
Also, you are not comparing them, you are performing a bitwise AND.
1000000110110110 = 33206 (dec)
0000000010000000 = 0x80 (hex)
AND ------------------------
0000000010000000 = result (bin)
Since the result is non-zero, this means that the particular file is owner-writable.
But what does the code you give do?
Well, it returns true
if and only if the file is writable by any of the following:
is_writable
)This certainly looks like illogical and buggy to me, because e.g.:
true
; however, that certainly does not mean the file is writable for you, unless you are the owner!is_writable
check would be superfluous and at the very least confusing.Upvotes: 2
Reputation: 798744
Where do this value come form?
They're hexadecimal equivalents of the permission bits, normally specified in octal.
Is this is a predetermined value for permission system?
And can someone explain to me for example how
$perms & 0x0080
resolve to true, cause for examplefileperms(__FILE__)
return a value like 33206
>>> hex(33206)
'0x81b6'
>>> 0x81b6 & 0x0080
128
Non-zero values are true.
Upvotes: 2