How to Set Default File Path for Include Files?

Included/required files will be searched in include path when not found in current directory or given path. This Path will act like a default path for all files included using include, require, require_once and include_once.

For lots of files residing in a deep directory and you want to set include path for all those files, you can set it using ini_set() or set_include_path().

For getting all the included path in PHP.ini, use php_ini('include_path') or get_include_path().


<?php
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/var/www/html/inc');
?>

Now, file will be searched in "/var/www/html/inc" folder when not found in current diectory
or given path. If file could not be found in given path and also not in included path also, then you will get error that File not found.

Do not add "/" at the end of path. No error generates for wrong path in included path.
ini_set('include_path', 'file path') will not modify your php.ini settings. Its effect is
for the script lifetime only.

Restore included path setting to the ini.ini setting value.


<?php
	ini_restore('include_path');
?>

Include Path setting only affect include/require and not other functions like fopen() or file_exists().

Comments are open for an year period. Please, write here on Facebook page.