Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get filename of file which ran PHP include

Tags:

include

php

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?

like image 595
enchance Avatar asked May 01 '12 03:05

enchance


1 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.

like image 110
animuson Avatar answered Oct 05 '22 22:10

animuson