I am using this:
echo dirname(__FILE__);
which gives:
C:\UwAmp\www\myfolder\admin
However I am looking for path until:
C:\UwAmp\www\myfolder\
from current script. How can that be done ?
If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.
The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
To go back 1 folder level with __dirname in Node. js, we can use the path. join method. const reqPath = path.
You could do either:
dirname(__DIR__);
Or:
__DIR__ . '/..';
...but in a web server environment you will probably find that you are already working from current file's working directory, so you can probably just use:
'../'
...to reference the directory above. You can replace __DIR__
with dirname(__FILE__)
before PHP 5.3.0.
You should also be aware what __DIR__
and __FILE__
refers to:
The full path and filename of the file. If used inside an include, the name of the included file is returned.
So it may not always point to where you want it to.
You can try
echo realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
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