Skip to content
Advertisement

path issue with include in PHP

I have an index.php in root and a ‘includes’ folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.

I know that I should use “../config/config.php” but it works with ‘config/config.php’.

I included header and footer in index.php and I should use path according to the index that I included to ???

what if I include one file in several folders ???

Folder structure

Advertisement

Answer

Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include

DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(FILE). This directory name does not have a trailing slash
unless it is the root directory.

Include your file like this from within your header.php and footer.php:

include_once DIR.’/../config/config.php’;

do the same technique for other files

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement