Absolute Paths in PHP
Let's read a text file located in the same folder as our script:
<?php
echo file_get_contents('test.txt');
?>
Now let's add a slash at the beginning of the path:
<?php
echo file_get_contents('/test.txt');
?>
In this case, the path becomes not relative, but absolute. The file will be searched for from the root of the operating system. Of course, the file won't be found at this path because it is located in the folder with our website.
We can get the path from the root of the operating system to the folder with our website:
<?php
echo $_SERVER['DOCUMENT_ROOT'];
?>
We can add the obtained path to the filename we are looking for - and get the correct absolute path to our file:
<?php
echo file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/test.txt');
?>