PHP - fopen():无法打开流:Mac 上的权限被拒绝?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35717653/
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
PHP - fopen(): failed to open stream: Permission denied on a mac?
提问by Pending
I can't get it to work and create a simple file in the same directory.
我无法让它工作并在同一目录中创建一个简单的文件。
I'm using a Mac and text editor is Sublime Text, and the php version is 5.5.30.
我用的是 Mac,文本编辑器是 Sublime Text,php 版本是 5.5.30。
This is the code I'm executing:
这是我正在执行的代码:
<?php
$file = fopen( 'data/data.txt' , 'w' );
if( $file ) {
echo 'The file is open';
fclose( $file );
}else{
echo 'The file could not be open.';
}
?>
This is the error generated in the browser:
这是浏览器中产生的错误:
Warning: fopen(data/data.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/php_class/class_examples/open_and_close.php on line 17
警告:fopen(data/data.txt):无法打开流:第 17 行的 /Library/WebServer/Documents/php_class/class_examples/open_and_close.php 中的权限被拒绝
I searched online for an answer to the error and this is the solution I got to fix the problem:
我在网上搜索了错误的答案,这是我解决问题的解决方案:
Open the terminal, go to the directory where the script is located and enter the following command
打开终端,进入脚本所在目录,输入以下命令
sudo chmod 777 open_and_close.php
Now I did all of this and I'm still getting the same error. Any suggestions on what I'm doing wrong?
现在我做了所有这些,但我仍然遇到同样的错误。关于我做错了什么的任何建议?
Thank you.
谢谢你。
回答by Fakhruddin Ujjainwala
You have to run the command for : data/data.txt
and not for open_and_close.php
您必须为 :data/data.txt
而不是为运行命令open_and_close.php
sudo chmod 777 data/data.txt
sudo chmod 777 data/data.txt