The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object.
ReadLink. One command that you can use to capture the full address of a file or an executable is readlink. Readlink is typically used to capture the path of a symbolic link or a canonical file. However, readlink can also compute the absolute path given a relative path.
The pwd command displays the full, absolute path of the current, or working, directory.
To use full path you type sh /home/user/scripts/someScript . sh /path/to/file is different from /path/to/file . sh runs /bin/sh which is symlinked to /bin/dash . Just making something clear on the examples you see on the net, normally you see sh ./somescript which can also be typed as `sh /path/to/script/scriptitself'.
__FILE__
constant will give you absolute path to current file.
Update:
The question was changed to ask how to retrieve the initially executed script instead of the currently running script. The only (??) reliable way to do that is to use the debug_backtrace
function.
$stack = debug_backtrace();
$firstFrame = $stack[count($stack) - 1];
$initialFile = $firstFrame['file'];
echo realpath(dirname(__FILE__));
If you place this in an included file, it prints the path to this include. To get the path of the parent script, replace __FILE__
with $_SERVER['PHP_SELF']
. But be aware that PHP_SELF is a security risk!
The correct solution is to use the get_included_files
function:
list($scriptPath) = get_included_files();
This will give you the absolute path of the initial script even if:
Here are two test scripts; the main script and an included file:
# C:\Users\Redacted\Desktop\main.php
include __DIR__ . DIRECTORY_SEPARATOR . 'include.php';
echoScriptPath();
# C:\Users\Redacted\Desktop\include.php
function echoScriptPath() {
list($scriptPath) = get_included_files();
echo 'The script being executed is ' . $scriptPath;
}
And the result; notice the current directory:
C:\>php C:\Users\Redacted\Desktop\main.php
The script being executed is C:\Users\Redacted\Desktop\main.php
__DIR__
From the manual:
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__)
. This directory name does not have a trailing slash unless it is the root directory.
__FILE__
always contains an absolute path with symlinks resolved whereas in older versions (than 4.0.2) it contained relative path under some circumstances.
Note: __DIR__
was added in PHP 5.3.0.
If you want to get current working directory use getcwd()
http://php.net/manual/en/function.getcwd.php
__FILE__
will return path with filename for example on XAMPP C:\xampp\htdocs\index.php
instead of C:\xampp\htdocs\
dirname(__FILE__)
will give the absolute route of the current file from which you are demanding the route, the route of your server directory.
example files :
www/http/html/index.php ; if you place this code inside your index.php it will return:
<?php
echo dirname(__FILE__); // this will return: www/http/html/
www/http/html/class/myclass.php ; if you place this code inside your myclass.php it will return:
<?php
echo dirname(__FILE__); // this will return: www/http/html/class/
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