Absolute Paths to the Script Folder and File in PHP
Sometimes we need to get not the path to the site's root, but the path to the script's folder.
These will be different paths if the executed script is not in the site's root, but in a subfolder, for example, like this:
- /script/
- index.php
In this case, the path to the script's folder is stored
in the constant __DIR__:
<?php
echo __DIR__;
?>
You can also get the path to the script file itself
using the constant __FILE__:
<?php
echo __FILE__;
?>
Write code that will read the contents of a text file:
- /script1/
- /script2/
- index.php
- /script2/
- /dir/
- test.txt
Write code that will read the contents of a text file:
- /script1/
- /script2/
- /script3/
- index.php
- /script3/
- /script2/
- /dir1/
- /dir2/
- /dir3/
- test.txt
- /dir3/
- /dir2/