I'm relatively new to configuring Apache.
I have a PHP script that writes a JSON file based on values retrieved from $_GET
.
<?php
file_put_contents('State.json', "{ do: '" . $_GET['do'] . "' }");
echo "Success";
?>
I run that code by create an XHR request.
Ally.xhr('/Cream/Foam?do=someCommand');
The page it returns says failed to open stream: Permission denied on line 3.
<Directory "~/Dropbox/Web">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Those are the permissions given to the root server folder.
What do I need to change to allow PHP to write the file?
(I have pretty much no idea what the block above means.)
You can use chmod() to do this.
Apache still needs access so that it can serve the files, so set www-data as the group owner and give the group r-x permissions. If you have folders that need to be writable by Apache, you can just modify the permission values for the group owner so that www-data has write access.
You need to check if the user under which runs apache has permission to write into the directory.
So it's like this:
Your apache server is process. The process runs under some user (say www). The PHP runs under apache. So if you try to write into a directory in PHP it is the same as if the user www logs into the server and tries to create a file in the same directory. So check who is owner of that directory and which permission do it have. You can do it e.g. via ls -la
command. If www
will be owner of that directory, you will be 100% safe ...
You can try to set the permissions with
chmod function for php and set your directory to /var/www there you have normally enough permissions.
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