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:

In this case, the path to the script's folder is stored in the constant __DIR__:

php<?php
  echo __DIR__;
?>

You can also get the path to the script file itself using the constant __FILE__:

php<?php
  echo __FILE__;
?>
Задание 1

Write code that will read the contents of a text file:

  • /script1//script2/index.php
  • /dir/test.txt
Задание 2

Write code that will read the contents of a text file:

  • /script1//script2//script3/index.php
  • /dir1//dir2//dir3/test.txt