fopen:打开流失败:Mac上PHP的权限被拒绝


fopen: failed to open stream: Permission denied in PHP on Mac

我写了这段代码:

if (file_exists("testfile.rtf")) echo "file exists.";
else echo "file doesn't exist.";
$fh = fopen("testfile.rtf", 'w') or die("impossible to open the file");
$text = <<< _END
Hi buddies, 
I write for the first time
in a PHP file!
_END;
fwrite($fh, $text) or die("Impossible to write in the file");
fclose($fh);
echo ("writing in the file 'testfile.rtf' succeed.");

但是当我运行它的时候它说:

fopen(testfile.rtf): failed to open stream: Permission denied in/Applications/XAMPP/xamppfiles/htdocs/test/test1.php第64行无法打开文件

我在MAC上使用XAMPP在本地服务器上工作。

我发现了几个主题,有些人有同样的问题,并解决了它使用的东西叫"chmod 777",但我不知道这是什么意思,在哪里写这个,等等。

您看到的错误仅仅意味着您的php脚本没有权限打开testfile.rtf进行写入。

不要 "chmod 777"任何时候你得到的权限问题,这可能会引入安全问题以后!相反,给正确的用户正确的权限。

在本例中:

既然你正在运行XAMPP,那么你需要确保你的web服务器获得特定路径的写权限,你试图在其中创建新文件和'或编辑现有文件。

在OSX和apache的情况下,我相信web服务器的用户名为"_www"。

这里有一个小例子(你可能需要根据你的需要修改它):

sudo chown -R _www:_www /path/to/folder

上面的例子将赋予_www (webserver) /path/to/folder和其中所有文件的所有权。

另外:在进行任何基于命令行的管理之前,您可能需要阅读一些关于unix命令的知识,例如chownchmod等。

https://www.techonthenet.com/linux/commands/chown.phphttps://www.techonthenet.com/linux/commands/chmod.php

希望能有所帮助!