PHP脚本无法写入文件夹

时间:2019-12-09 12:56:30

标签: php apache fopen fwrite

我面临一个奇怪的问题。我有如下所示的简单php脚本

<?php
$myfile = fopen("/var/www/html/***/templates_c/testnewfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

我发现http服务器以apache用户身份运行。我已将该文件夹的所有权授予该用户。

如果我通过SSH进入服务器并以php write.php的身份运行脚本,则可以将新文件写入template_c文件夹,但是如果我通过example.com/write.php这样的浏览器访问脚本,则会收到错误消息-Unable to open file!

我不明白为什么会发生这种情况,该文件夹拥有正确的所有权,甚至我将它递归地设置为777只是为了尝试,但我仍然得到相同的结果

编辑

这是控制台中的代码段,显示了html文件夹的所有权

0 drwxr-xr-x. 2 root   root      6 Jun  9  2019 cgi-bin
4 drwxrwxr-x. 4 apache apache 4096 Dec  9 17:51 html

这也是显示用户Web服务器用户为apache的代码段

$ ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'
apache

0 个答案:

没有答案