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/]:
php_value include_path ".:/var/www/mywebsite.ext/uat/htdocs"
Then in .htacess stored in apache's htdocs root [and git repo]:
php_value auto_prepend_file "globals.php"
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:
define['DIR', __DIR__];
Then in each file am was now able to include/require necessary files dynamically:
require_once[DIR.'/file_folder_inside/apache's_htdocs/some-file.php'];
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
/var/www/mywebsite.ext/uat/htdocs/file_folder_inside/apache's_htdocs/some-file.php.
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:
require_once[DIR.'/../apache's_htdocs_parent_folder/some-file-stored-outside-htdocs-eg-snesitive-credentials.php'];
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:
Code language: CSS [css]
. ├── inc │ ├── footer.php │ └── header.php └── index.php
The header.php
contains the code of the header part:
Code language: PHP [php]
PHP __DIR__ Demo
The footer.php
contains the code of the footer part. It also shows the value of the __DIR__
constant:
Code language: PHP [php]
The index.php includes both header.php
and footer.php
files. It also shows the value of the __DIR__
constant:
Code language: PHP [php]
Home
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:
Code language: CSS [css]
. ├── admin │ └── dashboard │ └── index.php ├── config │ └── app.php ├── inc │ ├── footer.php │ └── header.php └── public └── index.php
The
config/app.php
contains the application’s configuration:
Code language: PHP [php]
The footer.php
contains the closing tags of the body
and html
elements:
Code language: PHP [php]
The public/index.php
file includes both header.php
and footer.php
files:
Code language: PHP [php]
Home
If you navigate to the //localhost/web/public/index.php
, you’ll see the following output when you view the source of the page:
Code language: PHP [php]
My Awesome App
Dashboard
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:
Dashboard
Code language: PHP [php]
When you browse the page //localhost/web/admin/dashboard/index.php
, you’ll see the following error:
Code language: PHP [php]
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
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:
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?