FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » relative v. absolute paths
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: relative v. absolute paths [message #173278 is a reply to message #173275] Fri, 01 April 2011 07:38 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  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
--
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: browser time
Next Topic: writing php scripts for fastcgi environments
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 22:20:01 GMT 2024

Total time taken to generate the page: 0.04772 seconds