Get Absolute Path of Initially Run Script

Get absolute path of initially run script


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!

How to get full path to executed file?

This should work:

$a = get_included_files();
echo $a[0];

Quote from get_included_files() documentation:

The script originally called is considered an "included file," so it
will be listed together with the files referenced by include and
family.

Test

<!-- main.php -->
<?php include "middle.php"; ?>

<!-- middle.php -->
<?php include "inner.php"; ?>

<!-- inner.php -->
<?php var_dump(__FILE__, $_SERVER["PHP_SELF"], get_included_files()); ?>

Output

string(21) "C:\inetpub\wwwroot\inner.php"
string(9) "/main.php"
array(3) {
[0]=>
string(20) "C:\inetpub\wwwroot\main.php" <-- this is what you are looking for
[1]=>
string(22) "C:\inetpub\wwwroot\middle.php"
[2]=>
string(21) "C:\inetpub\wwwroot\inner.php"
}

Reliable way for a Bash script to get the full path to itself

Here's what I've come up with (edit: plus some tweaks provided by sfstewman, levigroker, Kyle Strand, and Rob Kennedy), that seems to mostly fit my "better" criteria:

SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

That SCRIPTPATH line seems particularly roundabout, but we need it rather than SCRIPTPATH=`pwd` in order to properly handle spaces and symlinks.

The inclusion of output redirection (>/dev/null 2>&1) handles the rare(?) case where cd might produce output that would interfere with the surrounding $( ... ) capture. (Such as cd being overridden to also ls a directory after switching to it.)

Note also that esoteric situations, such as executing a script that isn't coming from a file in an accessible file system at all (which is perfectly possible), is not catered to there (or in any of the other answers I've seen).

The -- after cd and before "$0" are in case the directory starts with a -.

How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?

Use realpath

$ realpath example.txt
/home/username/example.txt

how to get absolute path from relative path in command line

You can try to GUESS what the absolute path is by examining the pseudo symbolic link /proc/PID/cwd where PID can be determined from the ps output: ls -l /proc/PID/cwd. But please note that it is just the current working directory and your path may be resolved against any other directory, so it is just a guess.

Another option, if your program opens some files using this relative path and in fact you need to determine the full path of such files, you can enumerate files currently open by some process using /proc/PID/fd pseudo directory: ls -l /proc/PID/fd. But, of course, the file can already be closed by the time of executing ls. And I don't know what it will show if the open file was moved to some other place.

How to get the absolute path of the current javascript file name

You can investigate script collection at:

var scripts = document.getElementsByTagName("script");

For each element in the returned scripts array you can access its src attribute.

The currently executing include file will always be the last one in the scripts array. So you can access it at scripts[scripts.length-1].

Of course this will only work at time of initial code run and would not be useful for example within a function that is called after initial script is loaded, so if you need the value available later, you would need to save it to a variable.

PHP - Get absolute path of a php script for a url working in local and in remote

place in your webroot index.php file at the top

define('URL', filter_var(rtrim('http'.(((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? 's' : '').'://'.$_SERVER['SERVER_NAME'].str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])), '/').'/', FILTER_SANITIZE_URL));

in an MVC setup, this will allow URL to always equal your web root.

Get the path of current script

In RStudio, you can get the path to the file currently shown in the source pane using

rstudioapi::getSourceEditorContext()$path

If you only want the directory, use

dirname(rstudioapi::getSourceEditorContext()$path)

If you want the name of the file that's been run by source(filename), that's a little harder. You need to look for the variable srcfile somewhere back in the stack. How far back depends on how you write things, but it's around 4 steps back: for example,

fi <- tempfile()
writeLines("f()", fi)
f <- function() print(sys.frame(-4)$srcfile)
source(fi)
fi

should print the same thing on the last two lines.



Related Topics



Leave a reply



Submit