4

There has to be something I'm overlooking but I can't seem to get my includes to work as expected using relative paths. In MAMP the DocumentRoot is configured in httpd.conf like this:

# MAMP DOCUMENT_ROOT !! Don't remove this line !!
DocumentRoot "/Applications/MAMP/projects/journalproject”

I bring up the site at http://localhost:8888/ and the includes work fine if I use a path like this:

<?php include('nav.php'); ?>

But, if I put my include file into a folder, the include doesn't show up on the page:

<?php include('/includes/nav.php'); ?>

This is the path of the include file: /journalproject/includes/nav.php

I'm calling it from here: /journalproject/journals/index.php

I can also get the include to work if I use a path like this:

<?php include('../includes/nav.php'); ?>

I'm not sure why I need to specify the path for the include when an anchor link has no trouble finding the same file using a relative path:

<a href="/includes/nav.php">Find nav include</a>

I'm not sure where to go from here. Any help would be greatly appreciated.

Russ M
  • 41
  • 3
  • relative to your doc root (where presumably your script is running from, or your include path is set). – fbas May 10 '15 at 23:25
  • Thanks for your help. I tried without success, even on a file that resides in the root folder. I think it was a misunderstanding of mine that the document root for Apache would also dictate paths for PHP. I tried this instead, and it works: – Russ M May 12 '15 at 01:42
  • glad you found some success. You can also specify an INCLUDE PATH in PHP and those paths will be searched for your includes/requires. This way you can specify files relative to the include path without being conscious of where your specific .php file that calls the include/require is located: http://php.net/manual/en/function.set-include-path.php i.e. `set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'])` – fbas May 12 '15 at 15:50

2 Answers2

0

I believe that the below doesn't work because it is being treated as an absolute path, as it starts with a "/" which tells PHP to look from the literal "/" directory in Unix Operating Systems: <?php include('/includes/nav.php'); ?>

Try: <?php include('includes/nav.php'); ?>

0

you can specify files relative to the include path without being conscious of where your specific .php file that calls the include/require is located:

http://php.net/manual/en/function.set-include-path.php

set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'])

fbas
  • 1,676
  • 3
  • 16
  • 26