Jump to content

Absolute/relative path with includes


earachefl

Recommended Posts

I found one post that related to this topic but I want to make sure I understand. If I have a site with the structure:

 

/root/

____pageone.php

____common/

___________base.php

___________header.php

____inc/

______constants.inc.php

____admin/

_________login.php

 

and base.php includes:

<?php
// Include site constants
include_once "../inc/constants.inc.php";
?>

and login.php includes:

<?php
include_once "../common/base.php";
$pageTitle = "Log in";
include_once "../common/header.php";
?>

and pageone.php includes:

<?php
include_once "common/base.php";
$pageTitle = "Page One";
include_once "common/header.php";
?>

the login page loads normally, but pageone.php is broken; I get error messages like

Warning: include_once(../inc/constants.inc.php) [function.include-once]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/rootdirectory/common/base.php on line 10
.

 

From what I understand, PHP doesn't want to include the same files from different relative paths; you either need to use absolute paths or change the directory structure so that the included files are accessed through the exact same relative path. Is this correct? I find that if I move pageone.php to the admin folder and make the relative path the same, it does work.

 

I have to say, I'm leery of using absolute paths; and if I'm understanding this correctly, you're really constrained on how you set up your directory structure. Comments?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.