Search code examples
phpurlrelative-pathserver-side-scripting

Including a PHP file, treats relative URLs from that directory


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.


Solution

  • 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.