PHP 即使有 777 权限也无法写入文件

发布于 2024-08-14 05:09:49 字数 1127 浏览 2 评论 0 原文

我试图将 php 文件写入驻留在同一文件夹中的文件。 php 文件和它试图写入的文件的权限都设置为 777(它是一个 Linux 服务器)以及它们所在的文件夹。每当我使用 'w' 或 'w+' 模式调用 fopen() 时,该函数只返回 false。它位于我学校的网络服务器上,因此我无法获得 root 访问权限以将文件的所有者更改为与 apache 相同的用户。有谁知道出了什么问题吗?

更新: 作为测试,我使用了以下代码:

$handle = fopen("test.txt", 'w');
if($handle === FALSE)
    echo "\nfailed";
else
    echo "\nsuccess";
fclose($handle);

现在启用错误报告的输出是:

Warning: fopen(test.txt) [function.fopen]: failed to open stream: Permission denied in /<snip>/public_html/test.php on line 58
failed
Warning: fclose(): supplied argument is not a valid stream resource in /<snip>/public_html/test.php on line 63

上面是我从 php 网站复制的一些代码,用于检查文本文件的权限及其报告 -rwxrwxrwx 的 fileperms() 函数

相关文件的 ls -al 输出

ls -al *test*
-rwxrwxrwx   1 mag5     30          1475 Dec  9 00:02 test.php*
-rwxrwxrwx   1 mag5     30             8 Dec  8 14:54 test.txt*

也是我不确定这是否重要,但我的学校使用称为安德鲁文件系统的东西(http://en.wikipedia.org/wiki/Andrew_File_System)。

Im trying to make a php file write to a file that resides in the same folder. Both the php file and the file its trying to write to have their permissions set to 777 (its a linux server) as well as the folder they reside in. Whenever I called fopen() with the 'w' or 'w+' mode, the function just returns false. Its on my school's webserver, so there is no way I can get root access to change the owner of the file to the same user as apache. Does anyone know whats wrong?

Update:
As a test, I was using this code:

$handle = fopen("test.txt", 'w');
if($handle === FALSE)
    echo "\nfailed";
else
    echo "\nsuccess";
fclose($handle);

The output now with error reporting enabled is:

Warning: fopen(test.txt) [function.fopen]: failed to open stream: Permission denied in /<snip>/public_html/test.php on line 58
failed
Warning: fclose(): supplied argument is not a valid stream resource in /<snip>/public_html/test.php on line 63

Above that is some code I copied from the php website for the fileperms() function which checks the permissions of the text file, and its reporting -rwxrwxrwx

The ls -al output of the relevant files is

ls -al *test*
-rwxrwxrwx   1 mag5     30          1475 Dec  9 00:02 test.php*
-rwxrwxrwx   1 mag5     30             8 Dec  8 14:54 test.txt*

Also Im not sure if this matters, but my school uses something called the Andrew File system (http://en.wikipedia.org/wiki/Andrew_File_System).

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

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

发布评论

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

评论(5

栀子花开つ 2024-08-21 05:09:50

Telanor,AFS是一个非常大的线索。

AFS(Andrew 文件系统)具有超越传统 UNIX 文件系统的全级别目录权限。检查目录的 AFS 权限以确保您能够访问该目录中的文件。此外,重要的可能不是您的权限,而是网络服务器的权限(或者更确切地说是运行它的用户 ID)。自从我使用 AFS 以来已经很长时间了,所以我不知道检查目录权限的命令。

Telanor, AFS is a very big clue.

AFS (Andrew File System) has a whole level of directory permissions beyond that of traditional unix filesystems. Check AFS permissions on the directory to make sure you have the ability to access files in the directory. Also it may not be your permissions that matter, but the permissions of the webserver (or rather the userid it's running under). It's been a long time since I used AFS so I don't know the commands offhand to check directory permissions.

窝囊感情。 2024-08-21 05:09:50

这样做:

$fh = fopen($filename, "a");

我想问题是您没有该目录的正确权限。当您尝试删除文件时,您需要在目录中具有写权限,“w”即可执行此操作。

或者,如果您需要截断/删除文件,请更改目录权限,以便您拥有写入权限。

Do this instead:

$fh = fopen($filename, "a");

I imagine the problem is that you don't have the correct permissions for the directory. When you attempt to delete a file you need write permission in the directory and "w" will do that.

Alternatively, if you need to truncate/delete the file, change the directory permission so you have write permissions.

神也荒唐 2024-08-21 05:09:50

php 很可能没有以与文件所有者相同的用户身份运行。您是否尝试过使用 php 在目录中创建一个新文件(只需在同一目录中创建一个随机命名的文件)?

More than likely php is not running as the same user as the owner of the file. Have you tried creating a new file in the directory using php (just make a randomly named file in the same directory)?

眸中客 2024-08-21 05:09:50

这可能会失败有几个原因。根据周围的信息,不是文件权限的问题。第一个,也可能是最有可能的,是您的 Web 服务器正在以对整个文件系统具有只读访问权限的配置运行。这可能是因为 NFS 是以只读方式安装的,或者是因为 PHP 或服务器被配置为阻止写入。

另外,请不要将文件设置为 777。即使是 666 也足够危险了。在学校服务器等共享环境中尤其如此。

此时,假设您对服务器环境的控制有限,您应该向管理员询问更多信息。

There's a couple reasons this could fail. Based on the information around, it isn't a problem with file permissions. The first, and possibly most likely, is that your web server is running in a configuration that it has read-only access to the entire filesystem. This could be because NFS is mounted read-only, or because PHP or the server is configured in such a way as to prevent writing.

Also, please, never set a file to be 777. Even 666 is dangerous enough. This is especially true in a shared environment like a school server.

At this point, assuming you have limited control over the server environment, you should ask your administrator for more information.

匿名的好友 2024-08-21 05:09:50

正如 MadCoder 所说。
来自 AFS 文档:

AFS ACL 与标准 Unix“所有者”权限结合使用。只有所有者权限对 AFS 文件访问有影响; “组”和“其他”的 Unix 权限不会影响 AFS 文件访问。
这些规则适用。具有适当 AFS 权限的用户可以:
仅当设置了 UNIX“所有者读取”模式时读取文件
仅当 UNIX 所有者设置了“读”和“写”模式时才写入文件
仅当 UNIX 所有者设置了“读取”和“执行”模式时执行文件

要设置AFS权限,需要使用fs setacl命令。

It is like MadCoder says.
From the AFS Docs:

AFS ACLs work in conjunction with the standard Unix "owner" permissions. Only the owner permissions have an effect on AFS file access; Unix permissions for "group" and "other" do not affect AFS file access.
These rules apply. A user with appropriate AFS permissions can:
read a file only if the UNIX "owner read" mode is set.
write to a file only if the UNIX owner "read" and "write" modes are set.
execute a file only if the UNIX owner "read" and "execute" modes are set.

To set the AFS permissions, you need to use the fs setacl command.

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