I have a file(File1) in the root directory(Root). There is also a directory(Dir) in the root directory which contains File2.


include'/*I need to include file1 here*/'

How do I include file1 in file2?

This has failed miserably:'(


And this


does not work.

The first1 takes


to be a file name. And the second once wants to believe dir is a directory in Dir.

Coming to think of it, this does work


It includes file1 if it is in a directory which is 1 level higher than the directory in which is contained the file that includes file1.


... And what more do I need if file2 was in dir0


Member Avatar


Tell you what, this 'relative' referencing does my head in, especially when you have files in different folders including the same file which itself includes other files.

How about a cheat absolute:

include($_SERVER['DOCUMENT_ROOT'] . "/path/to/file/file.ext");

You could create a constant in your main include file (e.g. config.php) which holds the hard-coded document root info as opposed to using a superglobal variable. You only really need to use this on '2nd generation include files' - the ones that are referenced by other include files. Does that make sense?

Suppossedly, I,ve been denied access to the root directory

// This is the directory tree
//And my root directory is public_html
Warning: include() [function.include]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/home/a2246674/public_html/Dir0/Dir1/dir2/file1.php) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp)
Member Avatar


OK, so does the $_SERVER not work for you? Have you got free hosting? If so, perhaps you should consider a paid account. If the superglobal doesn't work, I don't know of any substitute. Sorry.

Sometimes $_SERVER doesn't display the right path. /usr/local/apache/ etc. should not be there.

Do: echo __FILE__; . That should show you the real that should work.