I have some problems with the relative and absolute paths in php fopen. I have the following directories:
project:
scripts:
myscript.php
logs:
mylog.log
I want to open mylog.log
from myscript.php
and I don't know how to specify the path. I tried
fopen('../logs/mylog.log', "a")
but it won't work. Thanks.
LE: Thanks for you answers.
In php, there are a couple of global constants that could be of help to you. Namely, __DIR__
gives you the directory of the current file ('.'
just gives you the directory of the root script executing).
So what you want is:
fopen(__DIR__ . '/../logs/mylog.log', "a")
You can use $_SERVER['DOCUMENT_ROOT']
which gives the document root of the virtual host.
eg: $_SERVER['DOCUMENT_ROOT']."/log/mylog.log"
This should work:
fopen(__DIR__ . '/../logs/mylog.log', "a");
or in PHP < 5.3:
fopen(dirname(__FILE__) . '/../logs/mylog.log', "a");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With