文件权限混乱

发布于 2024-12-11 03:52:45 字数 647 浏览 0 评论 0原文

在 FrogCms 中找到这段代码

function isWritable($file=null) {
  $perms = fileperms($file);
  if (is_writable($file) || ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002))
    return true;
}

我很难理解这部分

(is_writable($file) || ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002))

经过一些研究我知道 0x0080 是所有者的权限,0x0010 是组的权限,>0x0002 是对其他人的许可。这个值从哪里来?这是权限系统的预定值吗?

有人可以向我解释一下 $perms & 是如何实现的吗? 0x0080 解析为 true,因为例如 fileperms(__FILE__) 返回类似 33206 的值。你如何才能 比较 33206 和 0x0080?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2024-12-18 03:52:45

您引用的常量(0x0080 等)并不具有您认为的确切含义。请参阅 fileperms 文档:

0x0080 = owner writable
0x0010 = group writable
0x0002 = world writable

另外,您不是比较它们,您正在执行按位AND

    1000000110110110 = 33206 (dec)
    0000000010000000 = 0x80  (hex)
AND ------------------------
    0000000010000000 = result (bin)

由于结果非零,这意味着特定文件是所有者可写的。

但是您给出的代码是做什么的?

好吧,当且仅当文件可由以下任何写入时,它才会返回true

  • :脚本运行的用户(由 is_writable 检查)
  • 拥有该文件的用户
  • 拥有该文件的组
  • 任何用户

这对我来说当然看起来不合逻辑且有问题,因为例如:

  • 如果只有所有者可以写入函数将返回的文件;但是,这当然并不意味着该文件对您来说是可写的,除非您是所有者!
  • 如果目的是检查任何人是否可以写入该文件(这实际上毫无意义),那么is_writable检查将是多余的,至少会造成混乱。

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:

  • the user that the script runs as (checked by is_writable)
  • the user that owns the file
  • the group that owns the file
  • any user

This certainly looks like illogical and buggy to me, because e.g.:

  • if e.g. only the owner can write the file the function will return true; however, that certainly does not mean the file is writable for you, unless you are the owner!
  • if the aim is to check if anyone could write the file (which is kind of pointless really), the is_writable check would be superfluous and at the very least confusing.
花辞树 2024-12-18 03:52:45

这个值从哪里来?

它们是权限位的十六进制等效项,通常以八进制指定。

这是权限系统的预定值吗?

是的。

有人可以向我解释一下 $perms & 是如何实现的吗? 0x0080 解析为 true,因为例如 fileperms(__FILE__) 返回类似 33206 的值

>>> hex(33206)
'0x81b6'
>>> 0x81b6 & 0x0080
128

非零值为 true。

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?

Yes.

And can someone explain to me for example how $perms & 0x0080 resolve to true, cause for example fileperms(__FILE__) return a value like 33206

>>> hex(33206)
'0x81b6'
>>> 0x81b6 & 0x0080
128

Non-zero values are true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文