开发者

PHP Cannot write to file even though it has 777 permissions

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).


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.


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.


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)?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜