Asked  7 Months ago    Answers:  5   Viewed   54 times

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?



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.

Wednesday, March 31, 2021
answered 7 Months ago

If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.

Instead, base it on a known path.

/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:

define('CWD', realpath(dirname(__FILE__) . '/../') );

dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins


From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php

Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

Wednesday, March 31, 2021
answered 7 Months ago

It seems nothing is concrete in the world of xhr. I therefore resulted to using the X-File-Name header if I found it but requiring a user submitted filename. Uploads are read from the input stream.

Saturday, May 29, 2021
answered 5 Months ago

Create a closure around the File to capture the current file. Then you can get the filename.

An example:

Closure to capture the file information.

function parseData(entries){
  for (var i=0; i<entries.length; i++) {
    reader.onloadend = (function(file) {
      return function(evt) {
        createListItem(evt, file)

And the called function gets an additional argument

function createListItem(evt, file) {
Sunday, August 1, 2021
answered 3 Months ago

The includes are evaluated from the location of the running script. When you include another file, you are essentially pulling the contents of that file into the running script at that place.

For files that should evaluate includes relative to the included file's location, you can do this:


include(dirname(__FILE__) . '/';
include(dirname(__FILE__) . '/../asdf/'

From the documentation:

__FILE__ is 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.

dirname Given a string containing the path of a file or directory, this function will return the parent directory's path.

[] []

Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :