What is __ 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. Show 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/):
Then in .htacess stored in apache's htdocs root (and git repo):
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:
Then in each file am was now able to include/require necessary files dynamically:
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
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: Summary: in this tutorial, you’ll learn about how to use the PHP Introduction to the PHP __DIR__ magic constantPHP 5.3 introduced a new magic constant called When you
use the Technically speaking, the Simple PHP __DIR__exampleSuppose you have the following directory structure:
The
The
The index.php includes both
The following shows the output on the web browser:
The Why using PHP __DIR__Suppose that you have a project with the following directory structure:
The
The
The
The
If you navigate to the
So the
The
When you browse the page
The error message shows that the header.php cannot load the When the Since the To fix this issue, you can use the
The This is because when PHP loads the Therefore, it’s a good practice to use the Summary
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 "/".
|