Page 1 of 1

includes with absolute paths

Posted: Mon Feb 13, 2006 6:09 pm
by vchris
I was wondering if there is a way to have absolute paths work in php includes? If so how?

Posted: Mon Feb 13, 2006 6:46 pm
by Christopher
You can use absolute or relative paths with include() and require(). Relative paths are from the directory of the currently executing script. For absolute paths just do:

Code: Select all

include('/the/path/to/my/file.php');
// or tragically
include('C://the/path/to/my/file.php');

Posted: Mon Feb 13, 2006 7:48 pm
by vchris
I've tried but I get these 2 errors:

Code: Select all

Warning: main(/includes/header.php): failed to open stream: No such file or directory in c:\inetpub\wwwroot\mysite\index.php on line 1

Warning: main(): Failed opening '/includes/header.php' for inclusion (include_path='.;c:\php4\pear') in c:\inetpub\wwwroot\mysite\index.php on line 1

Posted: Mon Feb 13, 2006 7:51 pm
by feyd
is /codingtuts/includes :arrow: c:\codingtuts\includes ? I doubt it is. Absolute paths means an absolute path within the file system, not relative to your document root. $_SERVER['DOCUMENT_ROOT'] can be used to base a path off it, or figure out what the relative pathing would be.. or alter the include_path (php.ini or dynamic) settings.

set_include_path()/get_include_path()

Some constants you should be aware of: PATH_SEPARATOR and DIRECTORY_SEPARATOR

Posted: Mon Feb 13, 2006 8:18 pm
by vchris
And again feyd you solved my problem :)

It's works perfectly! thx a million. ;)