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';
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";
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');
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);
Related
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';
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";
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');
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);
I recently moved to a cloud server from a dedicated server environment and I'm having trouble with root relative paths inside of a php include. When I include the entire URL, the php include works perfectly. But it does not work when I use the root relative path in a subdirectory. Ideas?
Working with URL
<?php include 'http://website.com/assets/includes/nav.php';?>
file location http://website.com/folder/page
Working, but only in root index.php file. Will not work in subdirectory
<?php include 'assets/includes/nav.php';?>
file location http://website.com/index
NOT Working with root relative path
<?php include '/assets/includes/nav.php';?>
file location http://website.com/folder/page
If the file is on the same server (which is probably the case 99.99% of the time) you shouldn't use a URL to include it. This is very inefficient because it will require an extra HTTP request by the server. And of course, what is outputted by the remote web server is what is included - not the PHP source code, but the parsed output. As well, anyone with access to the remote URL could inject code into your website. Instead you should be using the file system to access the files directly.
If you're including a file starting with a / then this is relative to the root file system, not the website root directory. That is why your third example wasn't working. For example, your web server could serve pages from /var/www/html, so to include a file from within that directory, you would need to either start it with /var/www/html/ or make the include path relative.
What I typically do is from a file located in the root of the application that is called on every request (e.g. configuration file):
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__);
Then in any other file, I can just include relative to the root directory for that app:
include 'path/to/subdirectory/file.php';
You just have to be aware of duplicate file names. For example, if you have:
/var/www/html
├── class
│ ├── controller
│ │ ├── include.php
│ │ ├── FruitController.php
├── include.php
And from within your FruitController.php file you include include.php, It will check the first path from get_include_path(), and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. So depending on the order you put in set_include_path(), it will either include /var/www/html/include.php or /var/www/html/class/controller/include.php
Another way to do this and also add explicit control to what you're including would be to put the following in a file in the app's root directory:
define('APP_ROOT', __DIR__);
Then in any other file just do:
include APP_ROOT . '/path/to/subdirectory/file.php';
This is my first time moving a locally-hosted PHP site to a hosting service. My site is built with the inc folder (which includes the database constants) outside of the web root, so on my local machine it's in
XAMPP/xamppfiles/htdocs/inc/
and the rest of the files, including the common/ directory, are in
XAMPP/xamppfiles/htdocs/mydomain/
Most of the pages call
include_once "common/base.php";
which lives inside
htdocs/mydomain/common/
and which includes the database constants by calling
include_once $_SERVER['DOCUMENT_ROOT'] . "/inc/constants.inc.php";
On my local site, $_SERVER['DOCUMENT_ROOT'] outputs "Applications/XAMPP/xamppfiles/htdocs/", and the inc files are found.
Unfortunately, on the DotEasy site, the files aren't being found. If I echo $_SERVER['DOCUMENT_ROOT'], the output is "/home/myusername/public_html".
I'm able to get to the inc/ directory by this:
$temp_path = $_SERVER['DOCUMENT_ROOT'];
$temp_path = str_replace('public_html', '', $temp_path);
include_once $temp_path . "/inc/constants.inc.php";
but is this a good idea? Am I wrong to worry about having the constants in the public_html folder?
Don't use $_SERVER['DOCUMENT_ROOT'].
Instead you should define the root directory based by one file, for example in the config file.
define('ROOT', __DIR__); //php >= 5.3
or
define('ROOT', dirname(__FILE__)); // php < 5.3
The use the ROOT as the base to include other file.
I have a file which name is load.php near index.php in root folder. So i only have two files in root. I like clear works like most.
Today i needed to define directories in one file name is define.php. But it is not in root, i've added it to settings folder in root folder.
So my files:
index.php (requires load.php)
load.php (requires settings/define.php)
settings (includes define.php)
Now i don't know how to get root folders name?
__DIR__ in define.php gives me .../home/settings folder but i want to get root or another folders name. So what is the way?
To find out the "root" directory from inside settings/define.php, this should be sufficient:
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..');
See it in action.
Note: the linked example uses dirname(__FILE__) instead of __DIR__ because the latter is only available on PHP >= 5.3, but is functionally equivalent.
Is it possible to require files (using php require) from the directory where my website is placed?
For example, my website is in the directory mywebsite which is in the root directory. There is another directory there. Can I require files from this another directory?
Sure, you can require files from anywhere that has the appropriate permissions.
This requires the file from the current directory (NOT always where the current PHP script is, so be careful of that):
require("whatever.php");
This will require whatever.php from somefolder which is in the current directory.
require("somefolder/whatever.php");
Finally, you can give an absolute path like this:
require("/var/www/includes/whatever.php");
Require from parent directory:
require("../includes/watherver.php");
It doesn't matter really where you get it from, provided you have the permissions set correctly, and PHP is configured in such a way to allow you to do so.
I have been using
require_once $_SERVER['DOCUMENT_ROOT'] . '/myfile.php';
It should be no problem to require from an arbitrary existing and readable directory.
Image you have:
/
--folder1
--folder2
and in folder1 is your index.php and in folder2 is your to_require.php
Then you could write:
require('../folder2/to_require.php')
That's because you can go up in your directory tree with ..