PHP: fopen: no such file or directory

I am trying to create a log file for my website. For this, I use the following code to try to open the file. Now the file does not exist yet, but the documentation states that adding the “+” flag ensures that the file is created if it does not exist.

$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 a stupid question, I am very new to PHP.

+6
source share
1 answer

fopen The second parameter "a +" can only create a file if the directory exists. Verify that the log directory exists. If not, use:

 mkdir($_SERVER['DOCUMENT_ROOT']."/logs/", 0777, true); 

(true is the key) before fopen ()

+10
source

Source: https://habr.com/ru/post/917341/


All Articles