get_included_files
Gets the names of all files that have been included using include , include_once , require or require_once .
Parameters
This function has no parameters.
Return Values
Returns an array of the names of all files.
The script originally called is considered an «included file,» so it will be listed together with the files referenced by include and family.
Files that are included or required multiple times only show up once in the returned array.
Examples
Example #1 get_included_files() example
include ‘test1.php’ ;
include_once ‘test2.php’ ;
require ‘test3.php’ ;
require_once ‘test4.php’ ;
foreach ( $included_files as $filename ) echo » $filename \n» ;
>
The above example will output:
/path/to/abc.php /path/to/test1.php /path/to/test2.php /path/to/test3.php /path/to/test4.php
See Also
- include — include
- include_once — include_once
- require — require
- require_once — require_once
- get_required_files() — Alias of get_included_files
User Contributed Notes 7 notes
As of PHP5, this function seems to return an array with the first index being the script all subsequent scripts are included to.
If index.php includes b.php and c.php and calls get_included_files(), the returned array looks as follows:
If you want to know which is the script that is including current script you can use $_SERVER[‘SCRIPT_FILENAME’] or any other similar server global.
If you also want to ensure current script is being included and not run independently you should evaluate following expression:
If this expression returns TRUE, current script is being included or required.
If you have a MAIN php script which you don’t want to be included by other scripts, you could use this function. For example:
main.php:
function blockit ()
$buf = get_included_files ();
return $buf [ 0 ] != __FILE__ ;
>
blockit () and exit( «You can not include a MAIN file as a part of your script.» );
print «OK» ;
?>
So other script couldn’t include main.php to modify its internal global vars.
It’s perhaps not clear from the existing docs that the returned list contains nested include files as well.
That is, if A.php includes B.php, and B.php includes C.php, the result returned when calling get_included_files() from inside A.php WILL contain ‘C.php’.
As is often the case, YMMV. I tried the __FILE__ and SCRIPT_FILENAME comparison and found this:
SCRIPT_FILENAME: /var/www/cgi-bin/php441
__FILE__: /raid/home/natpresch/natpresch/RAY_included.php
Gives one when the script is standalone and always more than one when the script is included.
This function aims to perform filtering of files that have been included :
function setIncludeFiles ( $arrayInc = array()) <
$incFiles = get_included_files ();
if(( count ( $arrayInc )> 0 )&&( count ( $incFiles )> 0 )) <
$aInt = array_intersect ( $arrayInc , $incFiles );
if( count ( $aInt )> 0 ) <
return false ;
>elseif( count ( $aInt ) < 1 ) <
foreach( $arrayInc as $inc ) <
if( is_file ( $inc ))
include( $inc );
else <
return false ;
>
>
>
>else <
return false ;
>
>
?>
Usage :
$toBeInclude = array( ‘/data/your_include_files_1.php’ ,
‘/data/your_include_files_2.php’ ,
‘/data/your_include_files_3.php’ ,
);
setIncludeFiles ( $toBeInclude );
?>
Return false if something goes wrong.
This is a great way to emulate Python’s ‘__name__ = «__main__»‘
if( get_included_files ()[ 0 ] === __FILE__ ) doStuff ();
?>
Something that’s not noted in the docs, if a file is included remotely and you do a get_included_files() in the include itself it will *not* return the document that included it.
?>
test3.php (server 192.168.1.11):
Array ( [0] => /var/www/localhost/htdocs/test/test3.php )
Which means you can use get_included_files() to help intercept and prevent XSS-style attacks against your code.
- PHP Options/Info Functions
- assert_options
- assert
- cli_get_process_title
- cli_set_process_title
- dl
- extension_loaded
- gc_collect_cycles
- gc_disable
- gc_enable
- gc_enabled
- gc_mem_caches
- gc_status
- get_cfg_var
- get_current_user
- get_defined_constants
- get_extension_funcs
- get_include_path
- get_included_files
- get_loaded_extensions
- get_required_files
- get_resources
- getenv
- getlastmod
- getmygid
- getmyinode
- getmypid
- getmyuid
- getopt
- getrusage
- ini_alter
- ini_get_all
- ini_get
- ini_parse_quantity
- ini_restore
- ini_set
- memory_get_peak_usage
- memory_get_usage
- memory_reset_peak_usage
- php_ini_loaded_file
- php_ini_scanned_files
- php_sapi_name
- php_uname
- phpcredits
- phpinfo
- phpversion
- putenv
- set_include_path
- set_time_limit
- sys_get_temp_dir
- version_compare
- zend_thread_id
- zend_version
- get_magic_quotes_gpc
- get_magic_quotes_runtime
- restore_include_path
PHP: Check If A File Was Included
How to get a list of included files and know if a certain file was included by PHP.
Sometimes you may want to know whether a file was included in a PHP script or CMS, to do that we can use the get_included_files function; this function will return an array of absolute paths to the files that has been included by PHP.
The returned list is not always a complete list, since it is possible to include files at different points in the execution process. If a file is included by a function or method, and this has not been called yet, then the file will also not exist in the included files list.
When working with WordPress, you may want to use a hook, as that might help get a more complete list of files; you should read more about this in our WordPress tutorials.
We can display the output of the get_included_files function like this:
print_r(get_included_files());
Or, if you want to show the output as plain text:
header('content-type: text/plain; charset=UTF-8'); print_r(get_included_files());exit();
Checking if a file was included
The get_included_files function will produce a list of included files, regardless of whether they have been included using require, include, require_once, or include_once
To understand the difference between these different statements you can read this article: Include, Require, Include_once, and Require_once
There are two easy approaches when you want to check if a file has been included, one is to use the get_included_files function, which is the recommended approach, and another is to use the include_once statement — but the latter does not work well, since it triggers a fetal error if a file has already been included.
Since get_included_files returns an array of paths, we can just loop over the array in order to check if the string contains the file path we are looking for:
$list_of_fules_arr = get_included_files(); $string_to_look_for = 'shortcodes.php'; foreach ($list_of_fules_arr as $file_path) if (false !== strpos($file_path, $string_to_look_for)) echo 'Found: ' . $file_path;exit(); > >
To instead show all included files:
foreach ($list_of_fules_arr as $file_path) echo $file_path . "\n"; >
Extract the file name from the file path
You will often only need to know the file name of the included files, but since a system might indeed include multiple files with the same name — at different locations — this is not recommended.
The base path should be safe to remove though, and this way you can still tell the difference between two files that happen to have the same name. To do that, we first need to determine the base path, which can only be done from the composition root (I.e. index.php) relying on the document root is not consistent. Instead we should do it manually, like this:
define('BASE_PATH', rtrim(preg_replace('#[/\\\\]#', '/', realpath(dirname(__FILE__))), '/') . '/');
Now we have defined a constant called BASE_PATH that we can use to «cut off» the irrelevant /var/www/some_site/. part of the included file paths:
foreach ($list_of_fules_arr as &$file_path) $file_path = str_replace($file_path, '', BASE_PATH); > print_r($list_of_fules_arr);
Array ( [0] => /index.php [1] => /wp-blog-header.php [2] => /wp-load.php [3] => /wp-config.php [4] => /wp-settings.php [5] => /wp-includes/version.php [6] => /wp-includes/load.php . )