⊗ppPmFSSAP 337 of 447 menu

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
  • /dir/
    • test.txt

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

  • /script1/
    • /script2/
      • /script3/
        • index.php
  • /dir1/
    • /dir2/
      • /dir3/
        • test.txt
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline