php 警告:file_put_contents():无法打开流:权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22791667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:16:13  来源:igfitidea点击:

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

phpmacosfilefilewriter

提问by Mona

Hi I am new to Mac and web development..

嗨,我是 Mac 和 Web 开发的新手。

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.

这些天我正在尝试使用 HTML 和 Javascript 的简单表单来制作一个简单的网页,并且该表单将由 PHP 处理。PHP 将访问 txt 文件,读取和写入该 txt 文件。

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

所有的 HTML 和 PHP 代码都在我电脑的 myname/Sites/3006 文件夹中。我做了一些研究,他们都告诉我我需要更改一些配置以使 txt 文件可写,但真的很困惑如何这样做。任何人都知道我可以遵循的一个很好的说明页面(对于 mac 环境)。

Thank you very much..

非常感谢..

回答by Daniele Pantaleone

On OSX Apache is run by the _wwwuser. 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:

在 OSX 上,Apache 由_www用户运行。您需要确保该用户有权写入您要修改的文件。为此,请打开终端窗口并键入:

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/3006folder 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)

根据要求填写您的管理员用户密码。这会将/Users/maoyang/Sites/3006文件夹的所有权更改为_www 用户(运行 Apache 的用户),因此网络服务器将对该文件夹(以及所有子文件夹和文件)具有写入权限