Asked  7 Months ago    Answers:  5   Viewed   35 times

I'm having trouble understanding the ruleset regarding PHP relative include paths. If I run file A.PHP- and file A.PHP includes file B.PHP which includes file C.PHP, should the relative path to C.PHP be in relation to the location of B.PHP, or to the location of A.PHP? That is, does it matter which file the include is called from, or only what the current working directory is- and what determines the current working directory?

 Answers

100

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");
Wednesday, March 31, 2021
 
EzzDev
answered 7 Months ago
87

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.

  1. You can set your include_path to some specific, unchanging directory, so you always have a fixed path to include to relatively.
  2. Better: construct absolute paths:

    include __DIR__ . '/../Login/file.php';
    

    This is always relative to the directory the file is in.

Wednesday, March 31, 2021
 
Octopus
answered 7 Months ago
32

Do not trust $_SERVER variables. In some environments they can be set/altered by the user making the request. It's much better to define the base path manually in your index/bootstrap file and use it when needed.

define('SYSTEM_PATH', __DIR__ . '/');

or on version of PHP before 5.3 you can do this

define('SYSTEM_PATH', dirname(__FILE__) . '/');

Now you can always know the path to your files.

require(SYSTEM_PATH . 'lib/class.php');

Both __DIR__ and __FILE__ are safe constants set by PHP and can be trusted.

You can autoload classes like this:

function __autoload($class_name)
{
    require SYSTEM_PATH . strtolower($class_name) . '.php';
}

In other news, I can't ever think of a good use for include(). If you need to use include you are doing something wrong.

Wednesday, March 31, 2021
 
michele
answered 7 Months ago
73

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.

Wednesday, March 31, 2021
 
freeMagee
answered 7 Months ago
85

In cases when I need to use relative paths I use the following syntax:

include (realpath(dirname(__FILE__)."/another_folder/myfile.php"));
Friday, July 23, 2021
 
Extrakun
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :