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 #173285 is a reply to message #173278] Fri, 01 April 2011 17:39 Go to previous message
Evolution is currently offline  Evolution
Messages: 14
Registered: April 2011
Karma:
Junior Member
On Apr 1, 12:38 am, "Álvaro G. Vicario"
<alvaro.NOSPAMTH...@demogracia.com.invalid> wrote:
> 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
> --

Thanks, Alvaro, that helped a lot. This is how I wrote it and
everything now works again:

<?php
session_start();
$html_title = $_SESSION['html_title'];
print "<title>$html_title</title>";
$docRoot = getenv("DOCUMENT_ROOT");
require_once $docRoot . "/library/php/javascript.php";
?>

It's really nice to have the primary javascript all in once place so I
don't have to have javascript code on every page that is not even used
for anything but one page.

Before I used the above, I tried to assign the result of realpath to a
variable but wasn't able to succeed. While PHP is mostly just like
every other programming language, it sure has its peculiarities. :)

Thanks again.
[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: Sat Nov 30 16:31:43 GMT 2024

Total time taken to generate the page: 0.03887 seconds