Hướng dẫn __dir__ in php

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.

Nội dung chính

  • Introduction to the PHP __DIR__ magic constant
  • Simple PHP __DIR__example
  • Why using PHP __DIR__
  • What is __ FILE __ in PHP?
  • What is dirname __ DIR __?
  • What is dirname (__ file __) in PHP?
  • How do I go back to the root directory in PHP?

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:

. ├── 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?

What is __ FILE __ in PHP?

__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in.

What is dirname __ DIR __?

__DIR__ : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__) . This directory name does not have a trailing slash unless it is the root directory.

What is dirname (__ file __) in PHP?

dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap. php resides. (Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__) .)

How do I go back to the root directory in PHP?

The chroot() function changes the root directory of the current process to directory, and changes the current working directory to "/".