Search code examples
php.htaccessinclude-path

PHP Path to Include Files


I have a site with an index.php. The index.php has a number of include files like

 <?php include_once('scripts/php/index_mainImageJoin.php');?>

I've created a new folder off the root called 'extrapages' and I'm going to have pages in there that have information relating to the site. I've added the include_once files like above and change the path and these hook up fine.

The problem I'm find is paths within the include files fail. eg: if an image or another include file is within the include it fails when run from the 'extrapages' folder. Pathing issue.

Is there a good way to deal with this? Can I change/set the path to the site root (www) for pages under 'extrapages' to one folder down by chance?

I could move these pages onto the root and they would run fine but I really don't want all the clutter on the root of the site.

Any ideas & thx


Solution

  • The key to any path problem is called absolute path

    • while creating hyperlinks for your site (including image sources), always start it from / followed by full correct path. And it never fail you.

    • same for the filesystem calls: always use absolute path. Your server usually provides you with very handy variable called $_SERVER['DOCUMENT_ROOT'] contains the point where filesystem meet web-server, pointing to your web root directory.

    So, when called from anywhere in your site,

    include $_SERVER['DOCUMENT_ROOT'].'/scripts/php/index_mainImageJoin.php';
    

    will point always to the same location