Warning: file_put_contents(): failed to open stream: Permission denied
Asked Answered
M

1

5

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

Matildematin answered 1/4, 2014 at 16:31 Comment(2)
The user PHP runs as doesn't have permission to write to that directory.Soap
Make sure you have proper permissions to the folder you are trying to put toTelangiectasis
B
12

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)

Bertie answered 1/4, 2014 at 16:37 Comment(4)
but after using this command, I am not able to save the changes I made to the html php files....Matildematin
is there any way to change back the ownership?Matildematin
Yes. Just replace _www:_www with your account username. If the problem is just doing changes to html/php files you can solve it by changing the permission bitmask, eg: chmod -R 777 *.php (after navigating with the terminal to the document root)Bertie
okie! this solve the problem! now i can change the php, and the php file can modify the txt file. Thanks a lot fenix. :)Matildematin

© 2022 - 2024 — McMap. All rights reserved.