php 无法在PHP中打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13297145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:07:21  来源:igfitidea点击:

Can't open file in PHP

php

提问by CodeGuy

I'm trying to open a file with PHPfor write access:

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

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

My browser is spitting back:

我的浏览器正在回吐:

can't open file

无法打开文件

I've tried to chmod -R 777 the folder where this PHP file is as well as where $myFile (a text file) sits. What else could be the problem?

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

It ends up being that there are permission errors when I turn error reporting on. When I do ls -la all.txt, I get -rwx------ 1 Myname staff 0 Nov 8 15:11 all.txt

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

回答by ZhukV

If the script is started from other directory, please try open file from the full URI.

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

Example:

例子:

$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';
}