无法打开文件.什么所有者应该/var/www


Not able to fopen a file. What owner should /var/www have?

我使用 Ubuntu 11.04。我的/var/www 有所有者和组 shin (这是我的名字)

我使用了一个简单的 fopen php

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper'n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner'n";
fwrite($fh, $stringData);
fclose($fh);

但它给出了"无法打开文件"的错误。

它是否与 www 目录的所有者和组有关?应该是根吗?还是万维网数据?

如果所有者无事可做,那么代码会给出错误?

提前谢谢。

你应该提供一个完整的 fopen 路径,而不仅仅是文件名,并确保运行 php 代码的用户也具有写入权限(chmod 到 666)。

$myFile = "/var/www/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

fopen 如果失败,则会返回警告,请打开错误报告以获取有关错误的更多详细信息。 在代码块之前添加以下行。

error_reporting(-1);

假设您正在运行 Web 服务器(如 Apache),服务器进程的所有者必须具有写入文件的权限,因为您以'w'模式打开。 这不一定基于所有权;也可以通过模式完成。 要了解文件模式,请运行:man chmod