php - 在 PHP 中多次获取文件权限似乎不起作用
问题描述
fileperms
多次调用 PHP 函数时,文件权限似乎不再正确显示:
chmod('file.txt', 0600);
if ((fileperms('file.txt') & 0777) === 0600) {} // this is true
chmod('file.txt', 0660);
if ((fileperms('file.txt') & 0777) === 0660) {} // this is false
chmod('file.txt', 0666);
if ((fileperms('file.txt') & 0777) === 0666) {} // this is false
文件的权限已更改,但调用fileperms
显示不同的值。是否发生了一些缓存?
解决方案
事实证明,PHP 会在第一次请求文件权限后缓存它们。调用clearstatcache将重置缓存的文件权限。
当您使用 stat()、lstat() 或受影响函数列表(如下)中列出的任何其他函数时,PHP 会缓存这些函数返回的信息以提供更快的性能。但是,在某些情况下,您可能需要清除缓存的信息。例如,如果在一个脚本中多次检查同一个文件,并且该文件在该脚本的操作期间有被删除或更改的危险,您可以选择清除状态缓存。在这些情况下,您可以使用 clearstatcache() 函数来清除 PHP 缓存的有关文件的信息。
所以这应该在fileperms
调用之间重置缓存。
chmod('file.txt', 0600);
if ((fileperms('file.txt') & 0777) === 0600) {} // this is true
chmod('file.txt', 0660);
clearstatcache();
if ((fileperms('file.txt') & 0777) === 0660) {} // this is true
chmod('file.txt', 0666);
clearstatcache();
if ((fileperms('file.txt') & 0777) === 0666) {} // this is true
推荐阅读
- scikit-learn - 如何从循环通过多个 alpha 的套索回归中提取给我最小均方误差的 alpha?
- python - 当 N 变化时,如何生成对数图显示数值计算结果和分析得出的结果之间的小数误差
- python - 当我尝试为不和谐机器人执行命令时,我不断收到错误消息
- flutter - 我需要有关 Flutter 中数据库和性能改进的建议
- flutter - 如何将数据从 Getx 控制器传递到 Flutter 中的 View?
- python - 从 pandas 数据帧中删除 NaT 和重复项以获取一系列日期时间值
- asp.net-core - 来自另一个项目的 ASP.NET Core 5 调用启动无法加载视图
- pine-script - PineScript - 彩条序列没有从它应该开始的地方开始
- javascript - 如何在 Typescript/React 中使用 Tailwind 动画增加进度条宽度
- node.js - mongoose 和 mogodb 获取与国家相关的所有国家和用户