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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With