4

我正在尝试使用PHP打开一个文件以进行写访问:

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

我的浏览器吐了:

无法打开文件

我试图 chmod -R 777 这个 PHP 文件所在的文件夹以及 $myFile (一个文本文件)所在的文件夹。还有什么问题?

当我打开错误报告时,最终会出现权限错误。当我这样做时ls -la all.txt,我得到-rwx------ 1 Myname staff 0 Nov 8 15:11 all.txt

4

1 回答 1

7

如果脚本是从其他目录启动的,请尝试从完整的URI打开文件。

例子:

$myFile = '/path/to/myFile.txt';
if (!file_exists($myFile)) {
  print 'File not found';
}
else if(!$fh = fopen($myFile, 'w')) {
  print 'Can\'t open file';
}
else {
  print 'Success open file';
}
于 2012-11-08T20:25:53.990 回答