How to Format a PHP Include() Absolute (Rather Than Relative) Path

How do I format a PHP include() absolute (rather than relative) path?

If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.

If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.

So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().

If you want to set your own include "root", have a look at this question (specifically my answer of course :-)

How do I set an absolute include path in PHP?

What I do is put a config.php file in my root directory. This file is included by all PHP files in my project. In that config.php file, I then do the following;

define( 'ROOT_DIR', dirname(__FILE__) );

Then in all files, I know what the root of my project is and can do stuff like this

require_once( ROOT_DIR.'/include/functions.php' );

Sorry, no bonus points for getting outside of the public directory ;) This also has the unfortunate side affect that you still need a relative path for finding config.php, but it makes the rest of your includes much easier.

Running a php file from another php file as if I opened it directly from the browser (relative path issue)

Presuming there is no client-side code being run in doAllTheThings.php and you know the path to doAllTheThings.php.

The main issue is how include resolves paths within nested include files.

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. [sic]

In this case the calling script is customInterface.php.

Being that include resolves relative paths from the current working directory of the executed script file, the simplest approach to circumvent the issue and make the calling script behave as if you executed doAllTheThings.php directly, you can use chdir in order to change the current working directory.

chdir https://3v4l.org/D0Tn6

<?php
//...

if (true) {
//check to make sure doAllTheThings.php actually exists and retrieve the absolute path
if (!$doAllThings = realpath('../../things/important/doAllTheThings.php')) {
throw new \RuntimeException('Unable to find do all things');
}

//change working directory to the same as doAllTheThings.php
chdir(dirname($doAllThings));

//include doAllTheThings from the new current working directory
include $doAllThings;

//change the working directory back to this file's directory
chdir(__DIR__);
}

//...
?>


include __DIR__ or dirname(__FILE__)

However, if possible, I strongly recommend using absolute paths by including the root path, by appending __DIR__ or dirname(__FILE__) in PHP < 5.3 to any relative include paths. This will absolve the need to use chdir() as a workaround, and allow PHP to resolve the correct paths to include, while also allowing the application as a whole to function on any system the scripts are executed from.

<?php
include __DIR__ . '/../importantThing1.php';
include __DIR__ . '/../importantThing2.php';
include __DIR__ . '/../importantThing3.php';


set_include_path

Another more complex approach is to specify the include file directories, by using set_include_path(). However, if you are not aware of the directory the nested include scripts are in, this would require you to parse the include file to check for them. As such I do not recommend this approach, albeit viable.

<?php
if ($doAllThings = realpath('../../things/important/doAllTheThings.php') {
//retrieve the directory names of the scripts to be included
$basePath = dirname($doAllThings);
$subPath = dirname($basePath);

//add the directories to the include path
$include_path = set_include_path(get_include_path() . PATH_SEPARATOR . $basePath . PATH_SEPARATOR . $subPath);

include $doAllThings;

//restore the include path
set_include_path($include_path);
}

How should I include a file where of I know the path, but don't want to use ../?

The easiest way to do this, is using the following script:

<?php
$docroot = $_SERVER['document_root'];
require_once($docroot.'/scripts/script.php');
?>

Important! The server has to be well-configured for this method. At least the document_root should be set properly.

php include working with URL, but not root path. Server configuration or coding syntax?

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';

how to add absolute path with php include

If you are using any framework, then follow that, otherwise you can do following:

$path = __DIR__;
include($path . '/connect.php'); // change this to match path

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");


Related Topics



Leave a reply



Submit