Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permissions set to 777 and file still not writeable

I have set file permissions to 777 yet I cannot write to the file with PHP.

I can clearly see in my FTP client that the file has 0777 permissions and when I do:

echo (true === is_writable('file.txt')) ? 'yes' : 'no';

I get 'no';

I also tried:

echo (true === chmod('file.txt', 0777)) ? 'yes' : 'no';

With the same result.

The directory listing goes something like this:

public_html
    public          0777
        css         0755
        js          0755
        file.txt    0777

And I'm using .htaccess file to redirect all traffic to the public subfolder. Of course, I have excluded the file from rewriting (it is accessible from the browser I checked):

RewriteRule  ^(file).*  - [L]

Why is that?

like image 392
Richard Knop Avatar asked Aug 11 '09 12:08

Richard Knop


1 Answers

I guess Apache runs as a different user/group than the user/group owning the file. In which case, the file itself needs to be 0777.

public only needs to be 0777 if you plan on adding files to the folder using PHP. Even if the folder itself is not 0777, if the file is and the folder has at least 5 for the user (read/execute), you should be able to write to the file.

In the end, your file tree should look like this:

public_html
    public
        file.txt  0777

Naturally, you won't be able to change those permissions using PHP, but you can do so from your FTP client.

If it still isn't working, PHP might be running in safe mode or you might be using an extension such as PHP Suhosin. You might get better result changing the owner of the file to the same user/group that is running the script.

To get the user/group id of the executing user, you may use the following:

<?php
echo getmyuid().':'.getmygid(); //ex:. 0:0
?>

Then, you may use chown (in the terminal) to change the owner of the file:

> chown 0:0 file.txt
like image 165
Andrew Moore Avatar answered Sep 24 '22 19:09

Andrew Moore