[UPHPU] include() outside root

Mac Newbold mac at macnewbold.com
Tue Feb 1 08:59:41 MST 2005


Today at 8:36am, Timothy Humphrey said:

> 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?

Yes, it is possible

> 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.

Thanks for the example. It helps a lot.

According to http://www.php.net/include :

"Files for including are first looked in include_path relative to the 
current working directory and then in include_path relative to the 
directory of current script. E.g. if your include_path is ., current 
working directory is '/www/', you included 'include/a.php' and there is 
include "b.php"  in that file, b.php is first looked in /www/ and then in 
/www/include/. If filename begins with ../, it is looked only in 
include_path relative to the current working directory."

That last sentence may have an effect here. One possible way to work 
around it might be to include "../config.php"  as "./../config.php" 
instead, so that it doesn't technically start with "../". An obvious 
workaround is to use absolute paths, though that may be undesirable in 
your particular case for reasons you haven't mentioned here.

You should make sure "." is in the include_path, too.

Don't know if it matters, but I'd probably recommend quotes around your 
filename/path inside include("file.php") too.

Another option would be to change working directory before and after 
including b.php, so that the current working directory is the same 
directory in which backend.php is actually located:

/root/website/backend.php
<? include("../config.php"); ?>

/root/website/file_1.php
<? include("backend.php"); ?>

/root/website/folder/file_2.php
<?
chdir("..");
include("backend.php");
chdir("folder");
?>

Let us know what works once you get it fixed... now you've got me curious 
:)

Thanks,
Mac

--
Mac Newbold		MNE - Mac Newbold Enterprises, LLC
mac at macnewbold.com	http://www.macnewbold.com/


More information about the UPHPU mailing list