When using the PHP include
, how can I find out which file is calling the include
? In short, what is the parent's file filename?

- 29,075
- 35
- 87
- 127
-
A better question is why do you need to know this? Perhaps there's a better way of explicitly doing what you're trying to accomplish. – Zimzat May 01 '12 at 04:10
-
I want to know this so that I can use an include to replace the whole head section of the document. In the head section I am dynamically creating the label by str_replacing the filename hyphens with spaces and uc words to cap it. Problem is, the file it is in is the included file not the parent. I am using the answer by @animuson (accepted answer) just in case anyone is still wondering why this might be desired. – DavidG Jul 23 '18 at 12:43
-
**Wadih M.'s answer is the correct answer** if albeit a bit messy. Most everything else just blindly goes to the parent *most* file, *not* the intermediary includes. I'd have up-voted the answer though the accepted answer is incorrect. – John Jul 09 '20 at 14:29
7 Answers
An easy way is to assign a variable in the parent file (before the inclue), then reference that variable in the included file.
Parent File:
$myvar_not_replicated = __FILE__; // Make sure nothing else is going to overwrite
include 'other_file.php';
Included File:
if (isset($myvar_not_replicated)) echo "{$myvar_not_replicated} included me";
else echo "Unknown file included me";
You could also mess around with get_included_files()
or debug_backtrace()
and find the event when and where the file got included, but that can get a little messy and complicated.

- 53,861
- 28
- 137
- 147
-
this is a workaround, not a answer. It would be nice if the author would remove it. be an example. – Apr 06 '21 at 16:18
$fileList = get_included_files();
$topMost = $fileList[0];
if ($topMost == __FILE__) echo 'no parents';
else echo "parent is $topMost";
I think this should give the right result when there's a single parent.
By that I mean the situation where the parent is not a required or an included file itself.

- 7,394
- 2
- 24
- 35
-
1this answer is wrong because get_included_files returns all files a file has included. The OP asked the opposite, he wanted to know witch file included the file. – Apr 06 '21 at 16:34
Late answer, but ...
I check the running parent filename by using:
$_SERVER["SCRIPT_NAME"] // or
$_SERVER["REQUEST_URI"] // (with query string)

- 6,415
- 4
- 36
- 34
-
1This should be more highly rated as this gives the correct answer without needing to edit the container files for an include. I have an include that needed the name of its container file and this sure beats changing the `$var` values in 100+ other pages as the other answers suggest. – Martin Jul 05 '15 at 09:55
-
5**HIGHLY INCORRECT ANSWER** ! `SCRIPT_NAME` returns the most first file. and `REQUEST_URI` is out of question! they wont help in any CMS. Use `debug_backtrace()` or `get_included_files()` – T.Todua Aug 03 '16 at 11:24
-
1@T.Todua these qualifiers are valid but are well out of the scope of this question (and answer). `$_SERVER['SCRIPT_NAME']` is the value needed here. – Martin Nov 18 '17 at 13:15
You could use debug_backtrace() directly with no additional changes from within the included file:
$including_filename = pathinfo(debug_backtrace()[0]['file'])['basename'];
This will give you the name of the file that's including you.
To see everything you have access to from within the included file, run this from within it:
print_r(debug_backtrace());
You will get something like:
Array
(
[0] => Array
(
[file] => /var/folder/folder/folder/file.php
[line] => 554
[function] => include
)
)

- 12,810
- 7
- 47
- 57
Got this from here: https://stackoverflow.com/a/35622743/9270227
echo "Parent full URL: ";
echo $_SERVER["SCRIPT_FILENAME"] . '<br>';

- 482
- 10
- 20
I was searching same information on web for complement my online course about php and found two ways. The first was
$file = baseline($_SERVER['PHP_SELF']);
echo $file; //that outputs file name
BUT, in include or require cases it gets the final file that it's included or required. Also found this, the second
$file = __FILE__;
echo $file; //that outputs the absolute way from file
BUT i just was looking for the file name. So... I mix it up and it worths well!
$file = basename(__FILE__);
echo $file; //that outputs the file name itself (no include/require problems)

- 29
- 2
In the parent file, add this line before including the child file:
$_SESSION['parent_file'] = $_SERVER['PHP_SELF'];
And then in the child file, read the session variable:
$parent_file = $_SESSION['parent_file']

- 1,358
- 5
- 24
- 30

- 17
- 1