Skip to main content
Known Participant
January 17, 2010
Question

Can 'basedir' help me use root-relative paths in PHP includes?

  • January 17, 2010
  • 2 replies
  • 816 views

Hi,

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

...doesn't work, presumably because PHP believes the root directory to be "/home/user/domain/etc" instead of simply "domain"

I'm a bit new to PHP, but wouldn't the "basedir" attribute fix this? And if so, how would it be used in this context, so that Dreamweaver can continue to recognize where the files are located without a testing server needed, and root-relative paths can be used on the server itself?

Thanks!

This topic has been closed for replies.

2 replies

David_Powers
Inspiring
January 18, 2010

mjyeager wrote:

I'm a bit new to PHP, but wouldn't the "basedir" attribute fix this?

No. PHP include commands require one of the following:

  • A fully qualified path
  • A document-relative path
  • A document located in the PHP include_path

Nothing else will work.

John Waller
Community Expert
Community Expert
January 17, 2010

Try

<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/_header.php'); ?>

$_SERVER['DOCUMENT_ROOT'] is not supported by all servers (e.g. a Windows server running in CGI mode) but it's a useful first step. But try it and see.

jyeager11Author
Known Participant
January 17, 2010

Thanks, but I specifically avoid $_SERVER['DOCUMENT_ROOT'] because Dreamweaver doesn't know what to do with it locally. It requires configuring a formal testing server, which I don't want to do.

Right now, I am editing my HTML/PHP files locally, then uploading those files to server. When editing locally with SPLIT or DESIGN view, DW does a good job of putting all the includes where they should go -- even when using root-relative paths -- but those root-relative paths result in "not found" errors once on the server (for reasons explained in the OP).

Document-relative paths (ie, "../../file.php") work on both the local machine AND the server. But I'd rather NOT use doc-relative paths if at all possible.

If "basedir" does what I suspect it does, then I think I may be able to have my cake (use root-relative paths) and eat it too (have those root-relative paths work on the server too).

Please let me know if I'm being in any way confusing.

John Waller
Community Expert
Community Expert
January 17, 2010

I'm afraid I don't know of any other way of using root relative links locally in PHP other than with $_SERVER['DOCUMENT_ROOT']

and a local testing server.

Any particular reason you're not using a local testing server?

There are other PHP experts on this forum who will know more.