Is there some way to get the name of the file that called the current one, either through a require() or include()?
I don't know of a way to do that, but if there is only one level of includes, $_SERVER['SCRIPT_FILENAME'] will tell you what you need to know.
That will return the file in which the code is, not the filename of the parent file I think So far I know if, there is no possibility to check it, unless if you put in the parent file the following. $parent_file = "parentfile.php"; // The current file include("childfile.php"); // The include, so you must put the above code above the include() or require() PHP: In the child file you can then call the parent filename with using the variable $parent_file. (The variable $parent_file doesn't has to be made global or so, it will automaticly pass). Hope this helps.
I would probably just set a variable as suggested (well, I would actually question the architecture, but anyway...). If you REALLY want it to be dynamic, you could look at the output of the debug_backtrace function and traceback until you find the parent file.
I've done something like this before. In the parent file, put: $currentFile = $_SERVER["PHP_SELF"]; $parts = Explode('/', $currentFile); $parentfile = $parts[count($parts) - 1]; PHP: And in the included file: echo $parentfile; PHP: Should work.
NinjaNoodles: that would only work for one level of inclusion, though. If FileA included FileB and FileB included FileC, FileC would still see 'FileA'. I guess that may be what the OP wanted but if that were they case, there's no real need for anything special, anyway, as PHP_SELF is available in FileC in the first place, right?
Like I said, it would only work for one level of inclusion as PHP_SELF relates to the script that is being executed. If you read my example, you would see that even if you had 1 file including another and that file including another file, that last file would still see the initial 'root' file, not the file that is including it.
Well, I don't have the time to code it ATM, but there's probably a way to dynamically generate variables like $parentfile2 if ( isset($parentfile) ), and $parentfile3, and so on...
Oh well. If you really wanted you could take my suggestion of using the output of debug_backtrace within a function and it would be pretty simple to do completely dynamically. I don't have time to do the code but it would be pretty easy.