move_uploaded_file() 权限不足,即使所有者 ID 与用户 ID 相同

move_uploaded_file() has insufficient permissions even though the owner id is the same as the user id

我创建了一个文件夹/home/UploadContent,其中owner:group 为apache:apache,与php 进程相同的组和用户。然后我使用 chmod 将权限设置为 775,这应该允许 php 将上传的文件写入该目录。我试过了,它给了我同样的权限不足错误。

准确的错误是"failed to open stream: Permission denied"。

我写了一个小 php 程序来测试是否: $Owner = fileowner("/home/UploadContent/"); $Current = exec("whoami"); $Id = exec("id -u " . $Current); echo ($Owner . " is owner, " . $Id . " is user.");

这总是returns“48 是所有者,48 是用户。”

这意味着 php 具有正确的写入、读取和执行权限,但它仍然会抛出错误。

[从评论中提升]

听起来像是 SELinux 问题...

在 bash 提示符下尝试 运行ning setenforce 0,然后重新 运行 脚本。如果这解决了问题,您需要配置 SELinux 以允许您的进程访问指定的文件夹。

您可以使用 setenforce 1 重新启用 SELinux。否则,SELinux 将在启动时重新启用。

在 Centos/RedHat 上,我使用 audit2allow to make appropriate policies by analysing the audit log. I have no idea what the standard is on other distros. This Wiki page 解释了 SELinux 的作用以及如果您希望您的服务器安全,它为什么很重要。

我不知道有什么方法可以通过配置文件配置 SELinux,但是您可以使用 chcon to change the security context of a specific resource (like a file). More details of how security contexts work can be found here.

最后,你可以disable SELinux entirely但是不推荐这样做。

正如@VicSeedoubleyew 在评论中指出的那样,有一个有用的清单可以解决这个问题和类似问题