Get filename of file which ran PHP include
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.
Get the current script file name
Just use the PHP magic constant __FILE__
to get the current filename.
But it seems you want the part without .php
. So...
basename(__FILE__, '.php');
A more generic file extension remover would look like this...
function chopExtension($filename) {
return pathinfo($filename, PATHINFO_FILENAME);
}
var_dump(chopExtension('bob.php')); // string(3) "bob"
var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"
Using standard string library functions is much quicker, as you'd expect.
function chopExtension($filename) {
return substr($filename, 0, strrpos($filename, '.'));
}
Find out the name of the last script that included the current one
get_included_files
should be useful. Place the following code in level 2 or level 3 file:
$files = get_included_files();
list($parent) = array_slice($files, -2, 1);
echo $parent;
How to detect if a file is being included or directly ran
Make the included scripts not accessible via HTTP at all. E.g. by protecting the subfolder or moving them above the document root.
If you cannot do that, define()
something like IS_INCLUDED
in your main script and exit;
if this constant is not defined()
in your included script.
Get code line and file that's executing the current function in PHP?
You can use debug_backtrace().
http://us3.php.net/manual/en/function.debug-backtrace.php
So, in your log function, you would be able to retrieve the filename and line number from which the log function was called.
I'm using this approach in my logging classes and it has significantly reduced the amount of code required to get meaningful log data. Another benefit would be readability. Magic constants tend to get quite ugly when mixed with strings.
Here's a quick example:
function log($msg)
{
$bt = debug_backtrace();
$caller = array_shift($bt);
// echo $caller['file'];
// echo $caller['line'];
// do your logging stuff here.
}
php includes is there any way to include a file relative only to that document?
This may help: (from http://php.net/manual/en/function.include.php)
Files for including are first looked
for in each include_path entry
relative to the current working
directory, and then in the directory
of current script. E.g. if your
include_path is libraries, current
working directory is /www/, you
included include/a.php and there is
include "b.php" in that file, b.php
is first looked in /www/libraries/
and then in /www/include/. If filename
begins with ./ or ../, it is looked
for only in the current working
directory or parent of the current
working directory, respectively
Your question states:
If I want to include another file
inside footer.php, I must do it
relative to the index.php file (the
one that is including it).
This is true only if the filepath you are trying to include()
starts with ./
or ../
. If you need to include a file above the current file using a relative path, you can (as you suggested) use:
include( dirname(__FILE__) . '/../file.php')
If you define an absolute path, you can also add this to the current include_path:
set_include_path(get_include_path() . PATH_SEPARATOR . '/absolute/path');
You can then do all your includes relative to '/absolute/path/'.
PHP: Check if a file is loaded directly instead of including?
If you use
define('APP_RAN');
in the file that includes it and then put
if(!defined('APP_RAN')){ die(); }
or alternatively
defined('APP_RAN') or die();
(which is easier to read)
in included files it would die if you access them directly.
It would probably be better to put all of your included files above your DocumentRoot though.
For example, if your index page is at
/my/server/domain/public_html
You should put the included files in
/my/server/domain/
Related Topics
Storing Database Connection in a Session Variable
Comma-Separated String to Array
PHP How to Retrieve Array Values
How to Run Multiple Insert Query in SQL Using PHP in One Go
MySQL - Insert Date Range into Date Columns If Dates Don't Overlap with Existing Ones
MySQL Select from Tables Based on Multiple Rows
How to Do Multiple SQL Statements in One MySQL_Query
Get Repeated Matches with Preg_Match_All()
Fatal Error - 'Mongo' Class Not Found
Sort Array in PHP by Value and Maintain Index Association
Format Text in Excel File via PHP
Codeigniter Sessions Not Working After Migration
How to Replace All Xhtml/HTML Line Breaks (<Br>) with New Lines
Explode() into $Key=>$Value Pair
PHP Linefeeds (\N) Not Working
Select Random File from Directory