Are PHP include paths relative to the file or the calling code?
It's relative to the main script, in this case A.php. Remember that include()
just inserts code into the currently running script.
That is, does it matter which file the include is called from
No.
If you want to make it matter, and do an include relative to B.php, use the __FILE__
constant (or __DIR__
since PHP 5.2 IIRC) which will always point to the literal current file that the line of code is located in.
include(dirname(__FILE__)."/C.PHP");
PHP will include a file relative to a calling script, but not if the include path contains ../
PHP's a bit odd in how it looks for files. If you include a file whose name starts with a slash or a dot, PHP ignores the include_path
entirely. If a dot, it assumes the name is relative to the script that kicked off everything (ie: the one the web server decided to run).
If you want to specify a path relative to the included script and not the startup one, what you really want is an absolute include that specifies the full name of the file. That's easy to do with the __FILE__
and __DIR__
constants, which reflect the name and directory of the currently running script.
include __DIR__ . '/../dirB/c.php';
If you like, you can also set the include_path
config setting to include the root of the app, and just specify all filenames relative to that.
Impossible to use include function when resource paths are relative
The simplest way to achieve this is with a root-relative path. If you begin every link with a "/" it is read as relative to the root directory. Therefore on mywebsite.com, a link to "/folder/file.php" will always go to "mywebsite.com/folder/file.php" regardless of how many folders deep the current page is.
This works great for live sites on a server. The problem I'm struggling to work out is it fails in my local XAMPP setup (my testing server) and on a shared hosting setup where the domain name has not gone live yet because in both cases the server maps the root directory to the wrong place but on a live site, it solves your problem perfectly.
PHP and paths - relative to file and not including code
There are two types of paths: relative, and absolute. Absolute paths start with a /..
(or C:/..
or however that works on Windows) and always unequivocally point at one specific file.
Relative paths (not starting with /
) are relative to the include_path
variable. That's a variable that contains a bunch of folders, where PHP will look for your relative path. That include_path
also includes .
, the current directory. The "current" directory depends on which file was the "first" to be executed. So it varies depending on which file starts to include
others.
- You can set your
include_path
to some specific, unchanging directory, so you always have a fixed path to include to relatively. Better: construct absolute paths:
include __DIR__ . '/../Login/file.php';
This is always relative to the directory the file is in.
PHP include relative path
You could always include it using __DIR__
:
include(dirname(__DIR__).'/config.php');
__DIR__
is a 'magical constant' and returns the directory of the current file without the trailing slash. It's actually an absolute path, you just have to concatenate the file name to __DIR__
. In this case, as we need to ascend a directory we use PHP's dirname
which ascends the file tree, and from here we can access config.php
.
You could set the root path in this method too:
define('ROOT_PATH', dirname(__DIR__) . '/');
in test.php would set your root to be at the /root/
level.
include(ROOT_PATH.'config.php');
Should then work to include the config file from where you want.
Include path ignored
I'd say you can't do that:
Files are included based on the file path given or, if none is
given, the include_path specified. [...]If a path is defined — whether absolute (starting with a drive letter
or \ on Windows, or / on Unix/Linux systems) or relative to the
current directory (starting with . or ..) — the include_path will be
ignored altogether.
Related Topics
Post Request in Laravel - Error - 419 Sorry, Your Session/ 419 Your Page Has Expired
PHP Warning: PHP Startup: How to Load Dynamic Library
How Create an Array from the Output of an Array Printed With Print_R
How to Upload Images into MySQL Database Using PHP Code
How to Bind MySQLi Bind_Param Arguments Dynamically in PHP
Laravel 5.2 Validation Error Not Appearing in Blade
What Kind of String Is This? How to Unserialize This String
PHP: Convert Any String to Utf-8 Without Knowing the Original Character Set, or At Least Try
Login to Remote Site With PHP Curl
PHP - Get Bool to Echo False When False
Using Jquery Ajax to Retrieve Data from MySQL
Isset() and Empty() - What to Use
Sending Email Via PHP Mail Function Goes to Spam
Forcing a Simplexml Object to a String, Regardless of Context