1

I'm trying to get the full path of the current executed file within PHP.

realpath() returns the path without the script filename.
pathinfo() you cannot use without passing it the whole path to break down.
__FILE__ gets the path of the calling file (ie; where the __FILE__ constant was called), which may be inside an included file; which I don't want.

The $_SERVER variables REQUEST_URI, SCRIPT_NAME & PHP_SELF don't returns the full path.

The most promising is the $_SERVER variable SCRIPT_FILENAME apart from the caveat..

If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.

I want exactly what $_SERVER['SCRIPT_FILENAME'] returns such as /home/site/public_html/index.php, but just not 100% sure if using that is safe to use in all instances?

Is the latter the best method to use? Apart from the CLI, will it always return the correct path for normal web requests?

Brett
  • 19,449
  • 54
  • 157
  • 290

3 Answers3

2

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"
}
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Nice! That seemed to do it! :) – Brett Oct 31 '12 at 08:10
  • An interesting difference between this and `$_SERVER['SCRIPT_FILENAME']` is this will return the 100% true path with backslashes on Windows systems and `$_SERVER['SCRIPT_FILENAME']` will return the path with forward slashes. :) – Brett Oct 31 '12 at 08:28
1

If you would like the current file you can retrieve it by using the global instance

__FILE__

See http://php.net/manual/en/language.constants.predefined.php for more info If you would like to retrieve the directory where the file is located

// PHP < 5.3 
dirname(__FILE__);

// PHP > 5.3 
__DIR__

See http://php.net/manual/en/language.constants.predefined.php and http://nl1.php.net/manual/en/function.dirname.php

EDIT: To retrieve the current executing script, you can get

$_SERVER['SCRIPT_FILENAME']

This will return the filename of the exectued file. See http://php.net/manual/en/reserved.variables.server.php for more info.

Bearwulf
  • 353
  • 1
  • 8
0

You can use dirname(__FILE__); or if you are on php > 5.3 just __DIR__;

Manual: http://php.net/manual/en/language.constants.predefined.php

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
  • That gives me the path of the file that that code was placed in, not the path of the executed file such as `$_SERVER['SCRIPT_FILENAME']` does. – Brett Oct 31 '12 at 08:00