Warning: file_put_contents(): failed to open stream: Permission denied

Mona picture Mona · Apr 1, 2014 · Viewed 35.2k times · Source

Hi I am new to Mac and web development..

These days I am trying to do a simple web page with a simple form with HTML and Javascript, and the form will be processed by PHP. The PHP will be access txt file, read from and write to this txt file.

The error message comes from this line of code.

file_put_contents("order.txt", $content);

and the error message is

Warning: file_put_contents(order.txt): failed to open stream: Permission denied in /Users/maoyang/Sites/3006/process.php on line 57

all the HTML and PHP codes are in the myname/Sites/3006 folder of my computer. I have done some research and all of them tells me that I need to change some configuration to make the txt file writeable, but really very confused how to do so. Anyone know a good instruction page I could follow(for mac environment).

Thank you very much..

Answer

Daniele Pantaleone picture Daniele Pantaleone · Apr 1, 2014

On OSX Apache is run by the _www user. You need to make sure that this user has permissions to write on the file you want to modify. To do so, open a terminal window and type:

sudo chown -R _www:_www /Users/maoyang/Sites/3006

Fill in your Administrator user password when requested. This will change the ownership of the /Users/maoyang/Sites/3006 folder to the _www user (the same one that run Apache) so the webserver will have writing permission on that folder (and all subfolders and files)