我试图将 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).
发布评论
评论(5)
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.
这样做:
我想问题是您没有该目录的正确权限。当您尝试删除文件时,您需要在目录中具有写权限,“w”即可执行此操作。
或者,如果您需要截断/删除文件,请更改目录权限,以便您拥有写入权限。
Do this instead:
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.
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)?
这可能会失败有几个原因。根据周围的信息,不是文件权限的问题。第一个,也可能是最有可能的,是您的 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.
正如 MadCoder 所说。
来自 AFS 文档:
要设置AFS权限,需要使用
fs setacl
命令。It is like MadCoder says.
From the AFS Docs:
To set the AFS permissions, you need to use the
fs setacl
command.