Wednesday, 12 September 2018

Get the included files with PHP

PHP's get_included_files() function returns an array containing a list of included files by your script, including the script itself. This post looks at the filenames returned in the array and corrects a couple of errors in the PHP documentation for this function.
Calling get_included_files() returns an array containing all the included files whether they were included with the include() include_once() require() or require_once() functions. It also includes the script itself, and any prepended scripts using the auto_prepend_file configuration directive (note that in the PHP documentation it says these are not included in the result). It does not include scripts included with the auto_append_file configuration directive.
For example, if you had the following script at /common/websites/test/example.php:
<?php
include('included.php');
include_once('included_once.php');
require('required.php');
require_once('required_once.php');
$included = get_included_files();
print_r($included);
The output of the above script would be:
Array
(
    [0] => /common/websites/test/example.php
    [1] => /common/websites/test/included.php
    [2] => /common/websites/test/included_once.php
    [3] => /common/websites/test/required.php
    [4] => /common/websites/test/required_once.php
)
If a file is included more than once it will be in the array just once.
If an included file did not exist and could not be included it will not be in the array.
The full path to the included files is included in the filenames. This is a second issue with the documentation on the PHP website: the example provided in the docs shows only the filename and not the full path as well. Perhaps this was the case in earlier versions of PHP, but at the very least in PHP 5.1.6 on CentOS and 5.2.6 on Debian it has the full path as well.

Related posts:

0 comments:

Post a Comment