PHP fopen($file, “w”) 权限被拒绝


PHP fopen($file, "w") permission denied

我的网络服务器上的php脚本有问题(Raspbian上的Apache/2.4.10),我无法在其上生成新文件。我已经尝试过chmod 777(这确实是不好的做法),但它仍然不起作用。我已经为此苦苦挣扎了几天,即使经过广泛的研究,我也无法找到解决这个问题的方法。下面是我的代码和它生成的错误消息:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$newwish = fopen("newfile.txt", "w+");
$txt = "LALALA";
fwrite($newwish, $txt);
fclose($newwish);
?>

警告:fopen(新文件.txt):无法打开流:权限被拒绝 在/var/www/html/handlers/wish.php 第 4 行

警告:fwrite() 期望参数 1 是资源,布尔值在 /var/www/html/handlers/wish.php 在第 6 行

警告:fclose() 期望参数 1 是资源,布尔值在 /var/www/html/handlers/wish.php 在第 7 行

我在 CLI 中尝试了您的脚本并且运行良好。

尝试使 www-data 成为文件夹和包含脚本的内容的所有者:

chown -R www-data:www-data phpscript/

然后授予 PHP 脚本文件权限:

chmod 644 index.php