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
https://forums.phpfreaks.com/topic/225107-absoluterelative-path-with-includes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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