How to Get Absolute Directory Path in PHP Script

Today, We want to share with you How to Get Absolute Directory Path in PHP Script.In this post we will show you php get directory of current file, hear for How to get absolute path on a web server using PHP we will give you demo and example for implement.In this post, we will learn about How to get the absolute path to the public_html folder with an example.

How to Get Absolute Directory Path in PHP Script

There are the Following The simple About How to Get Absolute Directory Path in PHP Script Full Information With Example and source code.

Read Also:  PHP Get Folder Path From File Path Examples

As I will cover this Post with live Working example to develop how to get selected file path in php, so the php get root directory of website for this example is following below.

Example 1:

using dirname(__FILE__)

<?php
  include(dirname(__FILE__)."/config.php");
?>

Example 2:

using new constant called __DIR__

<?php
  include(__DIR__."/global.php");
?>

Example 3:

absolute directory path in PHP

<?php
  echo __DIR__;
?>

Bonus

echo realpath(dirname(__FILE__));

//other way
echo getcwd();

//new 
print_r($_SERVER['DOCUMENT_ROOT']);


// absolute path with file name
echo $_SERVER['SCRIPT_FILENAME'];

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  How to Import and Export CSV Files Using Codeigniter and MySQL

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to Get Absolute Directory Path in PHP Script.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.