Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving PHP write permission in Apache

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

like image 649
McKayla Avatar asked Mar 18 '11 21:03

McKayla


People also ask

How do I give PHP permission to 777?

You can use chmod() to do this.

Does Apache need write access?

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.


2 Answers

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

like image 143
Jan Zyka Avatar answered Oct 14 '22 07:10

Jan Zyka


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.

like image 43
René Höhle Avatar answered Oct 14 '22 07:10

René Höhle