Get the included files with PHP
Posted June 4th, 2009 in 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:
- Reading through a directory with PHP (Thursday, June 18th 2009)
- How to check if a function exists in PHP (Friday, January 9th 2009)
- Check if a class exists with PHP (Thursday, December 11th 2008)
- How to use PHP's __autoload function (Wednesday, November 12th 2008)
- Javascript and CSS file timestamps with PHP (Thursday, November 1st 2007)
Subscribe / Follow / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email to have my posts sent in a daily email, follow me on Twitter or follow me on Facebook.
At least one new post is usually made every day. See my posting schedule for more details.
