[UPHPU] include() outside root
Wade Preston Shearer
lists at anavidesign.com
Tue Feb 1 09:09:32 MST 2005
> Is there a way to include() a file outside the document root from a
> file that is referenced in several files, each in a different level of
> the directory?
>
> I reread that question and realized that it's hard to understand; so
> I'll give an example:
>
> /root/website/backend.php
> <? include(../config.php); ?>
>
> /root/website/file_1.php
> <? include(backend.php); ?>
>
> /root/website/folder/file_2.php
> <? include(../backend.php); ?>
>
> In my experience, file_2.php gives an error finding config.php from
> backend.php on line 1.
The problem is that you are using relative paths for your including,
resulting in the "relativeness" becoming incorrect within the sub
directory.
You can set the absolute paths manually in each include, such as...
<?php include("/root/config.php"); ?>
<?php include("/root/website/backend.php"); ?>
...but that is not wise as you will have to go and change those paths
whenever the site is moved.
With PHP, you have the option to specify an include directory however.
This can be done in one of three places: (1) apache's httpd.conf, (2)
an .htaccess file, (3) or the php.ini file.
With this set, the path to...
"/root/"
...can be accessed by simply using...
<?php include("config.php"); ?>
..and the other includes can be set like...
/root/website/file_1.php
<?php include("/backend.php"); ?>
/root/website/folder/file_2.php
<?php include("/backend.php"); ?>
...noting how the file in the sub directory can access "backend.php"
without "backing out of the directory." This is because "/" is webroot.
More information about the UPHPU
mailing list