PHP __DIR__
Summary: in this tutorial, you’ll learn about how to use the PHP __DIR__
magic constant when including a PHP file.
Introduction to the PHP __DIR__
magic constant
PHP 5.3 introduced a new magic constant called __DIR__
. When you reference the __DIR__
inside a file, it returns the directory of the file. The __DIR__
doesn’t include a trailing slash e.g., /
or \
except it’s a root directory.
When you use the __DIR__
inside an include, the __DIR__
returns the directory of the included file.
Technically speaking, the __DIR__
is equivalent to the dirname(__FILE__)
. However, using the __DIR__
is more concise than the dirname(__FILE__)
.
Simple PHP __DIR__example
Suppose you have the following directory structure:
.
├── inc
│ ├── footer.php
│ └── header.php
└── index.php
Code language: CSS (css)
The header.php
contains the code of the header part:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP __DIR__ Demo</title>
</head>
<body>
Code language: PHP (php)
The footer.php
contains the code of the footer part. It also shows the value of the __DIR__
constant:
<p><?php echo __DIR__ ?></p>
</body>
</html>
Code language: PHP (php)
The index.php includes both header.php
and footer.php
files. It also shows the value of the __DIR__
constant:
<?php require 'inc/header.php' ?>
<h1>Home</h1>
<p><?php echo __DIR__ ?></p>
<?php require 'inc/footer.php' ?>
Code language: PHP (php)
The following shows the output on the web browser:
Code language: PHP (php)Home C:\xampp\htdocs\web C:\xampp\htdocs\web\inc
The __DIR__
in the index.php
returns the current directory of the index.php
C:\xampp\htdocs\web
while the __DIR__
in the footer.php
file returns the directory of the footer.php
file C:\xampp\htdocs\web\inc
.
Why using PHP __DIR__
Suppose that you have a project with the following directory structure:
.
├── admin
│ └── dashboard
│ └── index.php
├── config
│ └── app.php
├── inc
│ ├── footer.php
│ └── header.php
└── public
└── index.php
Code language: CSS (css)
The config/app.php
contains the application’s configuration:
<?php
define('APP_NAME', 'My Awesome App');
Code language: PHP (php)
The header.php
contains the header part of a page. It also includes the config/app.php
file and uses the APP_NAME
constant:
<?php require '../config/app.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo APP_NAME ?></title>
</head>
<body>
Code language: PHP (php)
The footer.php
contains the closing tags of the body
and html
elements:
Code language: PHP (php)</body> </html>
The public/index.php
file includes both header.php
and footer.php
files:
<?php require '../inc/header.php' ?>
<h1>Home</h1>
<?php require '../inc/footer.php'?>
Code language: PHP (php)
If you navigate to the http://localhost/web/public/index.php
, you’ll see the following output when you view the source of the page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Awesome App</title>
</head>
<body>
<h1>Dashboard</h1>
</body>
</html>
Code language: PHP (php)
So the index.php
page in the public
directory works as expected.
The index.php
in the admin/dashboard
also includes the header.php
and footer.php
files:
<?php require '../../inc/header.php' ?>
<h1>Dashboard</h1>
<?php require '../../inc/footer.php' ?>
Code language: PHP (php)
When you browse the page http://localhost/web/admin/dashboard/index.php
, you’ll see the following error:
Warning: require(../config/app.php): failed to open stream: No such file or directory in C:\xampp\htdocs\web\inc\header.php on line 1
Fatal error: require(): Failed opening required '../config/app.php' (include_path='\xampp\php\PEAR') in C:\xampp\htdocs\web\inc\header.php on line 1
Code language: PHP (php)
The error message shows that the header.php cannot load the '../config/app.php'
file.
When the index.php
in the admin/dashboard
directory loads the code from the header.php
, it’ll find the config/app.php
inside the admin
directory, not root
directory.
Since the admin
directory doesn’t have the config/app.php
file, PHP issues an error.
To fix this issue, you can use the __DIR__
when you include the config.php
in the header.php
file like this:
<?php require __DIR__. '/../config/app.php' ?>
Code language: PHP (php)
The index.php
in the admin/dashboard
will work correctly.
This is because when PHP loads the inc/header.php
file from either public/index.php
or admin/dashboard/index.php
, the __DIR__
inside the inc/header.php
always returns C:\xampp\htdocs\web\inc
.
Therefore, it’s a good practice to use the __DIR__
constant when you include a file.
Summary
- PHP
__DIR__
returns the directory of a file or the directory of the include file when the file is used as an include. - Use
__DIR__
when you include a file.