I tried to use the absolute path to include my files :
I have 4 files (I have other file on my localhost but oddly the inclusion works well) :
header.php
I tried to use the absolute path to include my files :
header.php (C:\wamp\www\MySite\layout\header.php)
The PHP code is executed on the server. An absolute path in this context means a file-system absolute path, not a web host path. Since you are on Windows, /pdo.php
in fact means C:/pdo.php
and not C:\wamp\www\MySite\pdo.php
as it seems you think.
The best way to work with paths in PHP, regarding include
and require
is to use the __FILE__ and __DIR__ magic constants and the dirname() PHP function to build the (file-system) absolute paths of files starting from their relative locations.
Your code becomes:
header.php (C:\wamp\www\MySite\layout\header.php
):
pdo.php (C:\wamp\www\MySite\pdo.php
)
dir1/dir2/dir3/file.php (C:\wamp\www\MySite\dir1\dir2\dir3\file.php
)
The solution presented here makes the code independent of its actual location in the file system. You can move the entire project (everything in C:\wamp\www\MySite
) in a different directory or on a different computer and it will work without changes. Even more, if you use forward slashes (/
) as directory names separators it works on Windows, macOS or any Linux flavor.