1

I know that sounds confusing, but how do you find the name of the file that another file is included into.

For example, I have two files a.php and b.php. b.php is included into a.php like so:

a.php:

include('b.php');

Now I want code to put into b.php to find the name of a.php. Does that make any sense?

EDIT: Ok, I have come up with a better way to rephrase the question:

How do you get the name of the including file (so in the above example, find the name of file a.php using code in b.php). Does this work?

basename($_SERVER['SCRIPT_FILENAME']);
Ignatius_Gim
  • 582
  • 2
  • 5
  • 21
  • Define further details, what u want? – user12 Feb 26 '14 at 11:37
  • Maybe explain __why__ you want this as well, as it sounds like a strange thing to need to do, there's _probably_ a better way. – naththedeveloper Feb 26 '14 at 11:38
  • 1
    I think this : http://stackoverflow.com/questions/10265216/get-calling-file-name-from-include might interest you. – Brice Feb 26 '14 at 11:39
  • I have a program that updates an sql database and after its done redirect the user to the form page. There are multiple form pages and so I need a dynamic link to each one of them. Does that make sense, its kind of hard to explain? – Ignatius_Gim Feb 26 '14 at 11:50

2 Answers2

0

Not sure why you might need it but it's possible using global __FILE__ constant:

echo basename(__FILE__);

From the documentation about __FILE__:

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • 1
    I think that he wants to know who included `b.php`. Using `__FILE__` in `b.php` will return the full path to `b.php`. As I said in my comment to his question, this has already been solved in an other question (considering I get what he asked :D) – Brice Feb 26 '14 at 11:43
  • @Brice ... and this path has all necessary info to be extracted with `pathinfo` or `basename` functions. – dfsq Feb 26 '14 at 11:44
  • This is the reverse of what was asked for. – BeNice Jan 22 '20 at 14:52
0

May be this is what you want

http://in1.php.net/get_included_files

try this function at end.

Rohit Awasthi
  • 686
  • 3
  • 12