PHP __DIR__ Demo

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.phpCode language: CSS (css)

The header.php contains the code of the header part:

 "en"> "UTF-8"> "viewport" content="width=device-width, initial-scale=1.0">   Code language: PHP (php)

The footer.php contains the code of the footer part. It also shows the value of the __DIR__ constant:

echo __DIR__ ?>

Code language: PHP (php)

The index.php includes both header.php and footer.php files. It also shows the value of the __DIR__ constant:

 require 'inc/header.php' ?> 

Home

echo __DIR__ ?>

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\incCode 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.phpCode language: CSS (css)

The config/app.php contains the application’s configuration:

 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:

 require '../config/app.php' ?> "en"> "UTF-8"> "viewport" content="width=device-width, initial-scale=1.0">   Code language: PHP (php)

The footer.php contains the closing tags of the body and html elements:

The public/index.php file includes both header.php and footer.php files:

 require '../inc/header.php' ?> 

Home

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:

 "en"> "UTF-8"> "viewport" content="width=device-width, initial-scale=1.0">   

Dashboard

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:

 require '../../inc/header.php' ?> 

Dashboard

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 1Code 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:

 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.

Источник

PHP Include from Root

That is a relative file path, but it begins at the root public directory of the site. That way it always references the same location, no matter what directory that code snippet ends up in. If it didn’t begin with that “/”, it would be a relative file path still but it would the location would depend on what directory you were in at the time. Might work fine when you are at /index.php, but if that moves to /contact/ , the file path breaks because the images folder is in the root not in /contact/ . Common sense stuff. But this gets a little more complicated when dealing with a server side language like PHP. You can also do includes with PHP like this:

Because it doesn’t begin with a “/”, it suffers from the same problem our images example suffered from. If that include code moves to a different directory, the reference can be broken. But with PHP, simply using that beginning “/” will not work, which can be mighty confusing. The problem is that PHP can see a bit “deeper” into your servers file system than HTML can. For example, the public web directory of CSS-Tricks actually lives at “/var/www/vhosts/css-tricks.com/httpdocs” on my server. So when you do an include with a “/” at the beginning, it looks WAY down deeper than you are intending it to. You actually want it to look in that public web directory. In my case, “httpdocs”. In order to get this functionality back and have relative file paths that do not change, use this bit of PHP smartness:

I have found that in some environments DOCUMENT_ROOT does not seem to be properly set. I have devised a way that is independent of the server, and whether the hosting provider provides the ability to set an include or auto-prepend path as well. Each directory contains a ‘meta’ file called ‘__php__.php’ (or whatever one wants to call it). For any files in that directory, they simply include it as . The file itself simply includes the one in the parent directory all the way up to the site root. The file in the site root then can include other files, a simply way of auto-including files even if a service provider does not support it, and also define a variable such as ‘SITE_ROOTDIR’ , which can then be used later. If the document files are moved to another directory, they will still include the __php__.php file in that directory and still get the SITE_ROOTDIR constant from the top __php__.php file. I also do something similar for a simple navigation bar, where each directory has a __navbar__.php file, and each page simply includes it at the correct location and it can include parent navigation elements and define its own navigation elements. One advantage of this is that a part of the site can be sectioned in a sub-directory, and still get the root of that part, even if it isn’t the actual document root. A disadvantage is that it may slow down while doing all the includes on a site with heavy traffic.

Источник

Читайте также:  Главная
Оцените статью