views:

364

answers:

4

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%5FFile%5FSystem).

+2  A: 

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.

cletus
fopen with the 'a' mode doesnt work either. Ive recursively set my entire /public_html/ directory to have 777 permissions and it still isnt letting me write to the directory from php
Telanor
And the directory permissions?
cletus
A: 

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

Link5
PHP is running under the http user, which is a different user than the owner of the file. Trying to create a file with php results in the same problem.
Telanor
A: 

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.

McPherrinM
+2  A: 

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.

MadCoder
You were right, it had to do with AFS-specific permissions. I found a page on our schools site that explained how to set permissions. I gave write permission to the http user (the apache user) and now it works. Thanks
Telanor