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

File2.php

<?php
include'/*I need to include file1 here*/'
//
?>

How do I include file1 in file2?

This has failed miserably:'(

include("../file1");

And this

include('dir/file_1.php');

does not work.

The first1 takes

../file1

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

include("../file1");

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.

Root/file1.php
Root/Dir/file2.php

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

Root/Dir/Dir0/file2.php

Member Avatar

diafol

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
/usr/local/apache/htdocs/home/a2246674/public_html/
//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

diafol

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.