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?
Answers
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
Phew.
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.
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.
Create a closure around the File
to capture the current file. Then you can get the filename.
An example: http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files
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)
};
})(entries[i]);
reader.readAsText(entries[i]);
}
}
And the called function gets an additional argument
function createListItem(evt, file) {
console.log(evt.target.result)
console.log(file.name);
}
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:
/foo/baz.php
include(dirname(__FILE__) . '/bar.inc.php';
include(dirname(__FILE__) . '/../asdf/qwerty.inc.php'
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.
[http://php.net/manual/en/function.dirname.php] [http://php.net/manual/en/language.constants.predefined.php]
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:
Included File:
You could also mess around with
get_included_files()
ordebug_backtrace()
and find the event when and where the file got included, but that can get a little messy and complicated.