检查文件权限
如何检查文件权限
,而无需通过passthru()
或exec()
运行操作系统特定命令?
How can I check file permissions
, without having to run operating system specific command via passthru()
or exec()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您想通过检查文件权限来做什么?
在编写安全代码时,“检查然后执行”任何事情几乎总是不正确的。 原因是,在检查是否可以做某事和实际执行某件事之间,系统的状态可能会发生变化,从而导致执行该操作会产生不同的结果。
例如,如果您在写入文件之前检查文件是否存在,则不要检查您是否成功写入该文件(或者没有以足够详细的方式进行检查),然后再根据您写入的文件的内容进行检查,您实际上可能正在读取攻击者编写的文件。
因此,不要检查文件权限,只要执行权限检查成功后要做的任何事情,并优雅地处理错误即可。
What do you want to do by checking file permissions?
When writing secure code, it's almost always incorrect to "check, then do" anything. The reason is that between the checking whether you can do something and actually doing it, the state of the system could change such that doing it would have a different result.
For example, if you check whether a file exists before writing one, don't check whether you wrote the file successfully (or don't check in a detailed-enough fashion), and then later depend on the contents of the file you wrote, you could actually be reading a file written by an attacker.
So instead of checking file permissions, just do whatever it was you were going to do if the permissions check succeeded, and handle errors gracefully.
decot(fileperms($dir) & 0777)
获取文件权限。示例: var_dump(decot(fileperms($dir) & 0777)); //返回值为'755'
decoct(fileperms($dir) & 0777)
to get file permission.Example: var_dump(decoct(fileperms($dir) & 0777)); //return is '755'
使用 fileperms() 函数和子字符串:
对于文件:
替换
__FILE__< /code> 和
__DIR__
与您的路径或变量Use fileperms() function and substring:
For file:
Replace
__FILE__
and__DIR__
with your path or variable真正的程序员使用按位运算,而不是字符串;)这是处理权限的更优雅的方式:
Real coders use bitwise operations, not strings ;) This is much more elegant way of handling permissions:
您可以使用 is_read(), is_executable() 等命令。
You can use the is_readable(), is_executable() etc.. commands.
使用 fileperms() 函数
Use fileperms() function