Directory structure,
A.php
folder
B.php
C.php
A.php includes B.php and B.php includes C.php.
But B.php needs to include C.php relative to A.php. Is there any way I can include C.php relative to B.php?
You can include using __DIR__ or dirname(__FILE__).
From the manual
The directory of the file. If used
inside an include, the directory of
the included file is returned. This is
equivalent to dirname(__FILE__).
try this
A.PHP
include_once(__DIR__.'/folder/B.php');
include_once(__DIR__.'/folder/C.php');
B.php
include_once(__DIR__.'/A.php');
Related
I have this directory tree:
test.php
foodir
`---- foo.php
bardir
`---- bar.php
When I open test.php, I include foo.php. Then, I want foo.php to include bar.php.
test.php:
include 'foodir/foo.php';
foo.php:
include '../bardir/bar.php';
However, when I open test.php, I get:
Warning: include(../bardir/bar.php): failed to open stream: No such file or directory
I noticed that if I change my directory tree to:
test.php
foodir
`---- foo.php
---- bardir
`---- bar.php
And then change foo.php to:
include 'bardir/bar.php';
Everything works. It appears that I can include files relative to the currently included file.
However, why am I not able to travel up the directory tree of that file?
Edit:
I know that I can put include 'bardir/bar.php' in foo.php. It would search for bar.php in the location of test.php. However, that doesn't solve my problem if I include foo.php from a file in a directory other than the one where test.php is. That's because in that other directory, bardir/bar.php wouldn't exist.
When you use include 'foodir/foo.php'; that file is now basically running in the "scope" of test.php.
Therefore, the include inside that file include '../bardir/bar.php';
will go up one folder and THEN searching for bardir, which isnt there:
(it's looking for this arrangement:)
parent/test.php
parent/foodir
`---- foo.php
bardir
`---- bar.php
so, the correct include to use inside foo.php would be include 'bardir/bar.php';
in short: If you open test.php all include-paths used in any included file should be relative to the location of test.php - not to their actual location.
ps.: As mentioned in the comments: Includes are first checking the defined include-dir, only if there isn't a match, the path is considered "relative".
Simply do:
include dirname(__DIR__).'/bardir/bar.php';
__DIR__ is the absolute directory path of the file where this constant is used/called (no matter where it's included from), thus __DIR__ is equal to /full/absolute/path/foodir. and dirname(__DIR__) goes one directory up of the path so dirname(__DIR__) /full/absolute/path, and where the bar.php resides is /full/absolute/path/bardir. That's what we want, that's what we get.
This way you don't have to worry include path relativity. It will work any case, no matter where foodir/foo.php is included from
"When you use include it assume the base directory to look for
file is the directory of current file"
So when you have to include file from the another dir you have to specify the relative path from parent directory as here "Base" to include so let say for
Base -> A
->file1.php
->B
->file1.php
we will use
include "../A/file1.php";
for Adding files from A and vice-versa.
Assume ../ as the one level up of specified dir
also you can use __DIR__ like build in constants for getting the dir name of current file
For example, I always see autoloaders called like this:
require_once __DIR__ . '/../vendor/autoload.php';
What is the difference between that and the more concise
require_once '../vendor/autoload.php';
?
PHP scripts run relative to the current path (result of getcwd()), not to the path of their own file. Using __DIR__ forces the include to happen relative to their own path.
To demonstrate, create the following files (and directories):
- file1.php
- dir/
- file2.php
- file3.php
If file2.php includes file3.php like this:
include `file3.php`.
It will work fine if you call file2.php directly. However, if file1.php includes file2.php, the current directory (getcwd()), will be wrong for file2.php, so file3.php cannot be included.
The current accepted answer does not fully explain the cause of using __DIR__ and in my opinion the answer is wrong.
I am gonna explain why do we really need this.
Suppose we have a file structure like this
- index.php
- file3.php -(content: hello fake world)
- dir/
- file2.php
- file3.php - (content: hello world)
If we include file3.php from file2.php and run file2.php directly, we will see the output hello world.
Now when we include file2.php in index.php, when the code will start executing and it will see file2.php again including file3.php using include 'file3.php', at first the execution will look for file3.php in the current execution directory (which is the same directory where index.php is present)..Since file3.php is present in that directory, it will include that file3.php instead of dir/file3.php and we will see the output hello fake world instead of hello world.
If file3.php would not exist in the same directory, it would then include the correct dir/file3.php file which makes the accepted answer not valid because it states file3.php cannot be included which is not true. It is included.
However, here comes the necessity of using __DIR__. If we would use include __DIR__ . '/file3.php' in file2.php, then it would include the correct file even though another file3.php is present in the parent directory.
For include its possible to set some folders where PHP search automatically. When you include a file with a relative path you search in all of that folders. Its better to define the real path to prevent some errors in loading wrong files.
https://secure.php.net/manual/en/function.set-include-path.php
Then you can be sure that you load the correct file.
Lets say I have two files in directory new_folder: a.php and b.php
In the file of a.php there is a statement:
include_once 'b.php'
In addition, I have a file demo.php in a parent folder, in the demo file there is a statement:
include_once 'new_folder/a.php'
I.E. the folder structure is:
demo.php
new_folder
- a.php
- b.php
Why if I write in the a.php file:
include_once 'b.php' - correct path
include_once './b.php' - incorrect path
include_once 'new_folder/b.php' - incorrect path
Try This
require_once('../b.php');
my answer is based on php.net:
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
that means that if I write in a.php: include_once 'b.php' ; and then include a.php in demo.php then first it looks for the file in the include_path, then it will look for the file in the directory of the calling script (a.php), and only then it will look for the file in the working directory (that is the directory of the file that we run and starts all the includes- demo.php)
edit: I tried running it and found out that first if there is b.php in the working directory then it will include it and not the b.php from the calling scrip's own directory.
the last check for the file is in the new_folder (the calling script's own directory)
Here is my directory tree:
/
index.php
include/
functions.php
head.php
connect.php
sub/
index.php
In my head.php and connect.php both have:
include_once 'include/functions.php';
My index.php in the root folder includes these two: head.php and connect.php like this:
include_once 'include/connect.php';
include_once 'include/head.php;'
However, when my index.php in sub/ includes functions.php and head.php, they would fail to also include functions.php.
Here's how I included in the sub/index.php:
include_once '../include/connect.php';
include_once '../include/head.php';
If I change in the head.php and connect.php to: include_once '../include/functions.php';
The sub/index.php would include everything normally but the index.php in the root would fail to load the functions.php.
How can I fix this?
PHP version: 5.2.*
Use the constant __DIR__ in your include statements and then move relative to that. So in sub/index.php you would do include_once __DIR__ . '../include/connect.php'
The __DIR__ is a constant that is the directory of the file that you are in.
http://php.net/manual/en/language.constants.predefined.php
If you are using php < v5.3, you can use dirname(__FILE__) to get the same thing.
The Error
Include statement error in head.php and connect.php
include_once 'include/functions.php';
The Fix
include_once 'functions.php';
OR
include_once __DIR__ . 'functions.php'; //PHP 5.3 or higher
OR
include_once dirname(__FILE__) . 'functions.php'; //PHP 5.2 or lower
The Reason
head.php and connect.php are located in the same folder as functions.php
As suggested by #Schleis, using __DIR__ (PHP 5.3+) or dirname(__FILE__); (PHP 5.2-) will allow for relative file includes.
I would suggest to set your web project root directory in every file using chdir() function, so you don't need to think about it where are you currently located and how many back-dirs ../ you need.
Use example:
chdir($_SERVER['DOCUMENT_ROOT']);
You could define constant for include path in root file and then use that constant in all other files:
define( "INCLUDE_PATH", dirname(__FILE__) . '/include/' );
// some other file
include_once INCLUDE_PATH . 'functions.php';
It is good practice to have one file like config.php in the root folder where are defined global settings like include paths etc. That way you do not have to care about relative paths anymore, and if in the future you decide to change the folder structure, instead of changing the paths in all files just change the include constant.
I have two files on my server.
File a.php:
<?php
die('this is my text');
?>
File b.php:
<?php
file_get_contents('http://mysite.pl/a.php');
?>
But it's not working... I can't use file_get_contents, when the files are located on the same server, I don't know why.
PHP Info:
allow_url_fopen: ON
allow_url_include: OFF
When I try use the code from file b.php on difficult server - it working... ;/
You could try using 127.0.0.1, however it won't work on a VirtualHost.
You can try the relative path.
If a.php and b.php are in the same directory, do:
// b.php (If a.php is in the same directory)
file_get_contents('./a.php');
If a.php is in the upper directory:
// b.php (If a.php is in an upper directory)
file_get_contents('../a.php');
If a.php is in the root directory:
// b.php (If a.php is in the root directory)
file_get_contents('/a.php');