Home »
Imported messages »
comp.lang.php »
relative v. absolute paths
Re: relative v. absolute paths [message #173278 is a reply to message #173275] |
Fri, 01 April 2011 07:38 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma:
|
Senior Member |
|
|
El 01/04/2011 7:28, Evolution escribió/wrote:
> I have a php file that when I include it from my homepage, I specify:
>
> require_once './library/php/head.php';
>
> head.php itself includes another php file in the middle of its text:
>
> require_once './library/php/javascript.php';
>
> That works and is all well and good.
>
> However, if I then try to use head.php from other php files in
> subdirectories, then the path to javascript.php is no longer valid and
> fails.
Relative paths are relative to the location of the _main_ script(*). You
can make a simple test to prove it. Add this to your head.php file:
<?php echo realpath('./') . PHP_EOL; ?>
Then require head.php from files at:
- /test.php
- /library/test.php
- /library/php/test.php
The easier way to understand it is to think of include/require as a sort
of copy & paste: the main script incorporates to itself the source code
of the included files and it runs exactly the same as it was physically
on the main file.
(*) Unless you chdir() to another directory.
> If I were to specify an absolute path to javascript.php, it should
> work from any other location.
>
> I'm a beginner at PHP so I have always abided by the idea that all php
> files should be included with a relative path. Is this always true?
>
> If so, is there a better way to do this? Thanks a bunch.
Some people provide no path at all and rely on the include_path php.ini
directive. I particularly find it easier to just provide full paths at
all times. You can use $_SERVER['DOCUMENT_ROOT'] if your host provides
it and it's reliable or build a DOC_ROOT constant:
require_once DOC_ROOT . 'library/php/javascript.php';
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
|
|
|
Goto Forum:
Current Time: Sun Nov 10 15:40:08 GMT 2024
Total time taken to generate the page: 0.04687 seconds