php – How to use __dir__?
php – How to use __dir__?
You can use __DIR__
to get your current scripts directory. It has been in PHP only since version 5.3, and its 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 projects 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 files upper directory, this is the best solution:
<?php
include __DIR__ . /../warlock.php;
Ive been looking to use an executed in apaches 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 apaches 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 apaches htdocs root (and git repo):
php_value auto_prepend_file globals.php
Both of the above can be set in .htacess although it wouldnt work well for multiple environments suchas as DEV, UAT, PRODUCTION in particular when using git repo.
In my globals.php file inside apaches htdocs I have defined a variable called DIR thats 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/apaches_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/apaches_htdocs/some-file.php.
Now, I was looking to access a php file thats sitting outside my apaches htdocs root folder which is also easily achievable by using:
require_once(DIR./../apaches_htdocs_parent_folder/some-file-stored-outside-htdocs-eg-snesitive-credentials.php);
php – How to use __dir__?
Related posts on PHP :
- php – How to get cookie value
- php – Difference between a normal ajax and long polling
- php – mysqli_real_escape_string – example for 100% safety
- php get URL of current file directory
- floating point – How to convert float value to integer in php?
- apache – How to install php 5 on Ubuntu 16.04
- php – Invalid column count in CSV input on line 1 Error
- php – Mailer Error: SMTP connect() failed.
- encryption – Correct way to use php openssl_encrypt
- php – Whats the net::ERR_HTTP2_PROTOCOL_ERROR about?