我在 lubuntu 上运行 apache 服务器,并尝试使用 php 编写用户可以下载的文本文件。我更改了权限,如下所示:
sudo chmod 775 /var/www -R
但是当我执行脚本时仍然出现错误:
Warning: fopen(3): failed to open stream: Permission denied in /var/www/myPage.php on line 217 Could not open file!
这是php代码:
$filename = $liste[0][0];
$fh = fopen($filename, "x+") or die("Could not open file!");
fwrite($fh, "foo") or die("Could not write to file");
fclose($fh);
我需要更改其他权限吗?还是有另一种方法可以做我想做的事情?
谢谢
最佳答案
写入文件夹需要 Apache 用户对该文件夹具有写入、读取和执行权限。
如果 www-data 既不是文件的所有者也不是文件组,那么您应该更改其中任何一个。完成此操作后,如果您使用 www-data 和 root 以外的用户(如“webeditor”)访问服务器,并且该用户既不是所有者也不是组中的用户,您可能会发现自己无法访问 web 文件夹。
我建议:
chown -R www-data:www-data /var/www
usermod -a -G www-data webeditor
find /var/www -type d -exec chmod 755 {} \;
find /var/www -type f -exec chmod 644 {} \;
关于php创建文件的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36765391/