PHP: fopen: 没有那个文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10877007/
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
PHP: fopen: No such file or directory
提问by Heshan Perera
I am trying to create write a log file for my web site. To do this I use the following code to try and open the file. Now the file does not exist yet, but the documentation states that adding "a+" flag ensures that the file is created if it does not exist.
我正在尝试为我的网站创建写入日志文件。为此,我使用以下代码尝试打开文件。现在该文件尚不存在,但文档指出添加“a+”标志可确保在文件不存在时创建该文件。
$file = fopen($_SERVER['DOCUMENT_ROOT']."/logs/mylogfile.txt", "a+");
The above code gives me the following error...
上面的代码给了我以下错误...
Warning: fopen(E:/wamp/www/logs/mylogfile.txt) [function.fopen]: failed to open stream: No such file or directory
What am I doing wrong ? Please excuse me if this is stupid question, I am very new to PHP.
我究竟做错了什么 ?如果这是一个愚蠢的问题,请原谅我,我对 PHP 很陌生。
回答by Gavriel
fopen's 2nd parameter "a+" can only create the file if the directory exists. Make sure the logs directory is there. If it's not the case use:
fopen 的第二个参数“a+”只能在目录存在时创建文件。确保日志目录在那里。如果不是这种情况,请使用:
mkdir($_SERVER['DOCUMENT_ROOT']."/logs/", 0777, true);
(true is the key) before fopen()
(true 是关键) 在 fopen() 之前

