includes with absolute paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

includes with absolute paths

Post by vchris »

I was wondering if there is a way to have absolute paths work in php includes? If so how?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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');
(#10850)
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

And again feyd you solved my problem :)

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