I’ve been looking to use an executed in apache’s root htdocs _DIR_ variable and be able to include other scripts containing sensitive data such as database login credentials sitting outside it. I struggled a bit trying different options but the below is working really well.
Firstly, in my apache virtual host config I set/include a full linux path to apache’s htdocs (you can add more paths by appending at the end :/path/to/folder/):
Then in
.htacess stored in apache’s htdocs root (and git repo):
Both of the above can be set in .htacess although it wouldn’t work well for multiple environments suchas as DEV, UAT, PRODUCTION in particular when using git repo.
In my globals.php file inside apache’s htdocs I have defined a variable called DIR that’s globally used by htdocs php scripts:
Then in each file am was now able to include/require necessary files dynamically:
The DIR variable would
always resolve to /var/www/mywebsite.ext/uat/htdocs no matter where in the tree I call it in the above example producing
Now, I was looking to access a php file that’s sitting outside my apache’s htdocs root folder which is also easily achievable by using:
Summary: in this tutorial, you’ll learn about how to use the PHP __DIR__
magic constant when including a PHP file.
Tóm Tắt
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' ?> <h2>Home</h2> <p><?php echo __DIR__ ?></p> <?php require 'inc/footer.php' ?>
Code language:
PHP
(
php
)
The following shows the output on the web browser:
Home C:\xampp\htdocs\web C:\xampp\htdocs\web\inc
Code language:
PHP
(
php
)
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:
</body> </html>
Code language:
PHP
(
php
)
The public/index.php
file includes both header.php
and footer.php
files:
<?php require '../inc/header.php' ?> <h2>Home</h2> <?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> <h2>Dashboard</h2> </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' ?> <h2>Dashboard</h2> <?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.
Did you find this tutorial useful?