Jump to content

Recommended Posts

hi all,

i was wondering how to use absolute path in the include() statement.

i tried something like "/path" as i do on html links but it does not work.

i tried the use of $_SERVER['DOCUMENT_ROOT'] but it gives me the root in php.ini which is not correct because the resulting path is site/document_root.

 

can someone give me an hint?

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/271187-absolute-path-in-include/
Share on other sites

"/" refferences the domain root

"./" refferences the current directory

"../" refferences the direct parent directory

 

so, take for example the site example.com has 3 directories in it's web root - html, scripts and images

html has no directories in it

images has 2 directories in it - bground and buttons

script has 3 directories in it - css, php and js

php has 2 directories in it - includes and templates

 

to get from myScript.php in the php directory to the must_Include.php in the includes directory you would use one of the following :

include './includes/must_Include.php';
include '../php/includes/must_include.php';
include '/script/php/includes/must_include.php';

obviously, the second option is kind of silly, but should still work.

Not quite correct, I'm afraid.

 

For $_SERVER['DOCUMENT_ROOT'], include () and require () you're working against the local file system of the server, which means that / would reference the server's root folder; Not the web root. Normally you'll find your web root folder in "/home/{username}/www", "/home/{username}/public_html" or something like that.

 

So by taking the example web root structure above, and assuming the web root folder is named "www", the last include () should read as follows:

include '/home/cypher86/www/script/php/includes/must_include.php';

 

Alternatively, using $_SERVER['DOCUMENT_ROOT']:

include $_SERVER['DOCUMENT_ROOT'].'script/includes/must_include.php';

 

Only when talking about URIs does / reference the domain root, which is the same as the root web folder. And only because the web server is instructed to start looking in that folder, and to not go above it.

Edited by Christian F.

hi guys,

the problem is that if i follow your instructions there is something wrong.

follow an example:

suppose i have the following file system:

web server root

|

|----lib

|----another_dir

 

the script i'm working on is under "another_dir" in which i have to include a script under "lib".

so i type include('/lib/script.php');

php returns me an error of script not found. if i use instead include('../lib/script.php') the script is found.

 

why's that?

As I wrote in my post above, the web root is not the same as the server root. By sending a path that starts with / to include (), you're telling it to look in the server root.

 

I suggest you read the posts again, and pay close attention to the details and differences between the server paths and the URIs.

You don't. You have to use the correct local path to the file you want to have included.

What the web-server reports as the "root" (/) is actually $_SERVER['DOCUMENT_ROOT']. Which is why you have to use it when using absolute paths to include files. Or at least if you don't want to have to change the path every time you put the file on a new system, or otherwise change the location of the web root folder.

Edited by Christian F.

As stated earlier: Local path is not the same as an URL.

  • The web server translates an URL to a local path, when it fetches the document specified in the URL.
  • PHP works internally with local paths. This include require () and include () calls.
  • HTTP works with URLs. This include PHP's header ('Location: ...'); call, as it sends a HTTP header to the browser.

You have to separate the two in your mind, and apply the correct methods to the correct scenario.

Edited by Christian F.

If you have folders that contain files that you are commonly including in your scripts such as a lib folder, then rather than using the full server path to a file every time you want to include it I would setup an include path in a config file using set_include_path().

 

So, in a config.php file I would do the following:

 

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . '/lib/');
?>

 

Now, if I want to include a file that is in the lib folder, lets say foobar.php. All I need to do from any file in the website is simply write:

 

<?php
include($_SERVER['DOCUMENT_ROOT'] . '/config.php');
include('foobar.php');
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.