PHP: Get a list of included PHP files.

This is a PHP tutorial on how to get a list of files that have been included in your script. To retrieve this list, we will be using PHP’s inbuiltĀ get_included_files function.

A quick example:

<?php

//Require config.php once.
require_once 'config.php';

//Require a fake library for example purposes.
require_once 'fake_library.php';

//Get an array of the files that have been included.
//We achieve this by calling the function get_included_files()
$includedFiles = get_included_files();

//var_dump the array so we can see what is inside it.
var_dump($includedFiles);

In the PHP code above, we included two PHP files before calling the get_included_files function. In this case, the function returned the following array:

As you can see, the array will contain the full path to each file. In PHP 5 and above, the array will also contain the full path to the script that called the function, as it is technically considered to be an “included file”. In this case, that is index.php.

If a file is included multiple times throughout the script, it will only show up once in the array, as duplicates are automatically removed.

include_once

You do not need to use this function to check if a file has already been included or not. If you want to make sure that a file is only included once, then you should use theĀ include_once statement. The include_once and require_once statements will automatically check to see if a file has already been included before they attempt to include it.

Hopefully, you found this post to be helpful!