Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»How to Get Absolute Directory Path in PHP Script

    How to Get Absolute Directory Path in PHP Script

    RahulBy RahulJanuary 10, 20191 Min ReadUpdated:January 21, 2019

    PHP 4.0 introduced __FILE__ magic constant, which provides the full path and filename of the file.

    To get directory path only use dirname(__FILE__). For example to include other file in same directory use:

    PHP
    1
    2
    3
    <?php
      include(dirname(__FILE__)."/other.php");
    ?>

    Later the PHP version 5.3.0 and later versions introduced a new constant called __DIR__ which is the short form of the dirname(__FILE__). You can also use this in replacement of what we used earlier.

    Now, you can use above script like:

    PHP
    1
    2
    3
    <?php
      include(__DIR__."/other.php");
    ?>

    To simply print the absolute directory path of PHP script located in:

    PHP
    1
    2
    3
    <?php
      echo __DIR__;
    ?>

    PHP
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleDownload Fedora 29 – DVD ISO Images
    Next Article How to Create SFTP User without Shell Access on Ubuntu 18.04 & 16.04

    Related Posts

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:May 9, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install and Use PHP Composer on Debian 11

    Updated:February 16, 20224 Mins Read

    How To Install PHP (8.1, 7.4 & 5.6) on Debian 11

    Updated:February 16, 20224 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.