60

I want to use __dir__.

However, I can't find any good tutorial on how to set it up. I have my htdocs in Dropbox.

Does it work something like this?

 define(__DIR___, 'd:documents/dropbox/yolo/swag/htdocs/myproject/test/newtest/
 testphp/test_new/testincludes/1/new_folder/')

That is the directory where my project is located and it has sub folders. I want to include a file into another file that is in the parent folder.

Should I then just type:

 include'__DIR__/warlock.php'; 

Or do I have to type something like this?

 include '___DIR__/wow/newb/guidesfornabz/classes/casters/warlock.php'; 
Dikesh Gandhi
  • 447
  • 13
  • 22
JosefPP
  • 642
  • 1
  • 5
  • 11
  • I have posted an answer in another similar question which explains the reason of using `__DIR__` and what happens when we don't use `__DIR__` in include or require. https://stackoverflow.com/a/65976952/13976117 – Mehbub Rashid Jan 31 '21 at 08:15

3 Answers3

120

You can use __DIR__ to get your current script's directory. It has been in PHP only since version 5.3, and it's the same as using dirname(__FILE__). In most cases it is used to include another file from an included file.

Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies.

project
├── inc
│   ├── file1.php
│   └── file2.php
└── index.php

If we do include "inc/file1.php"; from index.php it will work. However, from file1.php to include file2.php we must do an include relative to index.php and not from file1.php (so, include "inc/file2.php";). __DIR__ fixes this, so from file1.php we can do this:

<?php
include __DIR__ . "/file2.php";

To answer your question: to include the file warlock.php that is in your included file's upper directory, this is the best solution:

<?php
include __DIR__ . "/../warlock.php";
morganbaz
  • 2,997
  • 1
  • 17
  • 30
  • 1
    @Howl I tried include "file2.php" in file1.php. It worked properly. Can you explain why should "we must do an include relatively to index.php" ? – Suraj Malinga Feb 12 '18 at 11:08
  • @SurajMalinga - If you go to file1.php and use an include, then the path is looked at from file1.php to file2.php to include it. But __DIR__ allows us to give file1.php the correct path to file2.php when file1.php is not the file being executed. The interpreter is looking at being inside the project folder. Then if file1 calls to file2 via include, the interpreter will first look for `require('file2.php')` inside the project folder, NOT the `inc` folder. – j_allen_morris Feb 17 '19 at 03:54
  • u wouldn't need __DIR__ , I think include './../warlock.php' should work fine without it – clockw0rk Jun 17 '20 at 12:28
  • __DIR__ gives the path from server_root, probably something in the likes of /var/www/somestuff/file1.php. depending on implementation, using a normal include looks up the files from entry-point of application, e.g. index.php. not very helpfull if your file is located 10 subfolders from there – clockw0rk Oct 22 '21 at 09:21
3

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');
webcoder.co.uk
  • 341
  • 3
  • 5
-1

This is an example of how to use __DIR__ and go to the parent directory in different PHP versions, so if you can recognize which one is old and which one is new:

For PHP < 5.3 use:

$upOne = realpath(dirname(__FILE__) . '/..');

In PHP 5.3 to 5.6 use:

$upOne = realpath(__DIR__ . '/..');

In PHP >= 7.0 use:

$upOne = dirname(__DIR__, 1);
Raskul
  • 1,674
  • 10
  • 26