views:

446

answers:

3

I have a PHP file at my server root.. index.php .. which include's .. DIR/main.php

Now .. DIR/main.php .. include's many nearby PHP files using relative URLs using .. include("./common1.php");

Any way I can change the relative-URL base path so when including "DIR/main.php" It can relatively access its nearby PHP files like "DIR/common1.php", instead of trying to find "common1.php" at the site root.

+2  A: 

Take a look at set_include_path

Edit: When appending paths to include_path be sure to use the PATH_SEPARATOR constant as it is intended to make your include path OS agnostic.

<?php

set_include_path(implode(PATH_SEPARATOR, array(
    get_include_path(),
    '/DIR1',
    '/DIR2/DIR3',
    dirname(__FILE__),
)));

?>
Ionuț G. Stan
A: 

In addition to lonut's answer, you can get the directory of the file using a combination of the __FILE__ constant and dirname().

$include_path = dirname(__FILE__);
set_include_path($include_path);
tj111
A: 

First, set the "relative-URL base path" to your directory

set_include_path( get_include_path() . '/DIR' );

Second, include your file!

require( 'main.php' );

That should work though I've not tested it.

Jenko