cypher86 Posted November 26, 2012 Share Posted November 26, 2012 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 26, 2012 Share Posted November 26, 2012 "/" 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 (edited) 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 November 26, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
cypher86 Posted November 26, 2012 Author Share Posted November 26, 2012 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? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 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. Quote Link to comment Share on other sites More sharing options...
cypher86 Posted November 26, 2012 Author Share Posted November 26, 2012 ok but how i do instrunct the include to manage the pat as a uri instead of the document root? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 (edited) 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 November 26, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
cypher86 Posted November 26, 2012 Author Share Posted November 26, 2012 i agree with you. but as you said if i want absolute path and use / or DEOCUMENT_ROOT it redirects me to (for exemple) http://localhost/var/www/myapp which is obiously wrong. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 You need to append the path relative to the web root onto the value of $_SERVER['DOCUMENT_ROOT'] $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/path/relative/to/webroot.php"; Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 (edited) 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 November 26, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 (edited) This has already been answered. Edited November 26, 2012 by AyKay47 Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted November 26, 2012 Share Posted November 26, 2012 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'); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.