homepage Welcome to WebmasterWorld Guest from 54.163.139.36
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
relative includes
noyearzero

5+ Year Member



 
Msg#: 3809607 posted 4:41 pm on Dec 17, 2008 (gmt 0)

i must call on the braintrust for this one...

root/file1.php

<?
include('folder1/file2.php');
?>

root/folder1/file2.php

<?
include('../folder2/file3.php');
?>

root/folder2/file3.php

<?
echo 'good';
?>

if i run file /folder1/file2.php it finds file3.php ok. however if i run file1.php it can't find file3.php. this is because according to the php site "If filename begins with ./ or ../, it is looked only in the current working directory."

i suppose a solution would be to rewrite the cwd on every script AND then switch it back after every includes. but that seems extremely unnecessary.

my include path is just ".;C:\php5\pear" on windows and its equivalent on apache. both are running php 5.

one thing i can't figure out is if is if i have this code

<?
include('/file.php');
?>

what does it resolve the base to?

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3809607 posted 6:53 pm on Dec 17, 2008 (gmt 0)

>> what does it resolve the base to?

It should be your document root.

realestatesteve

5+ Year Member



 
Msg#: 3809607 posted 7:17 pm on Dec 17, 2008 (gmt 0)

So if you put ../ before the url it was always default to the root?

The Contractor

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3809607 posted 7:25 pm on Dec 17, 2008 (gmt 0)

To refer to the root see next to the last message by Receptional Andy at [webmasterworld.com...]

[edited by: eelixduppy at 8:09 pm (utc) on Dec. 17, 2008]
[edit reason] added msg anchor [/edit]

noyearzero

5+ Year Member



 
Msg#: 3809607 posted 8:23 pm on Dec 17, 2008 (gmt 0)

using <?php include $_SERVER['DOCUMENT_ROOT'].'/footer.php'; ?> won't help me because it is a shared hosting server. (it is our server though so we can make any mods to it we want). thus all the sites on the servers are vituralhosts. and from what i can tell, you can't dynamically set a Document root with virtual hosts.

also i do know that:
<?php include $_SERVER['DOCUMENT_ROOT'].'/footer.php'; ?> and
<?php include '/footer.php'; ?>
do not resolve to the same thing. (at least with my virtualhosted configuration).

phpinfo reports that my documentroot is the default documentroot from the main httpd.conf. and not what is resolved by the virtualdocumentroot. however if i set documentroot in the virtualhost block it reports this correctly, but its not dynamic. it seems like that kind of defeats the purpose?

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3809607 posted 8:56 pm on Dec 17, 2008 (gmt 0)

why don't you just try echoing them to see which gives you the proper path?

echo $_SERVER['DOCUMENT_ROOT'].'/footer.php';

should give you the correct file if the footer is located in the root of the domain

echo '/footer.php';

should give the file if it is located at the root of the server itself, which I doubt

for includes you are always dealing with the server's filesystem and is not relative to where your website may be setup

>> you can't dynamically set a Document root with virtual hosts

if you couldn't set it how would apache know where to look for the site?

noyearzero

5+ Year Member



 
Msg#: 3809607 posted 5:10 pm on Dec 18, 2008 (gmt 0)

jatar,

first off, won't echoing '/footer.php' always output /footer.php regardless?

secondly when echoing the documentroot, it is not the directory from where the files are being served. it echo either the default documentroot for the server or the explicitly set documentroot within the virtualhost block. Both of these values being different than what the virtualdocumentroot resolves too. this value resolves correctly and serves the files from the correct location, however i have no way of every echoing that value with a variable.

so right off the bat i had the same question as you.... why does it know where to look for the files in the first place?

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3809607 posted 8:16 pm on Dec 18, 2008 (gmt 0)

>> won't echoing '/footer.php' always output

hahaha, sorry I got distracted somewhere in there, and I'm obviously an idiot

>> however i have no way of every echoing that value with a variable

hmmm, makes me think it's a config issue, honestly I have no idea, sorry, I would poke around in the apache docs to see if something is not configured quite right

Alcoholico

5+ Year Member



 
Msg#: 3809607 posted 10:55 pm on Dec 20, 2008 (gmt 0)

This should work.....

root/file1.php

<?php
if (!defined('ROOT_DIR')){
define('ROOT_DIR', dirname(__FILE__));
}
include(ROOT_DIR.'/folder1/file2.php');
?>

root/folder1/file2.php

<?php
if (!defined('ROOT_DIR')){
define('ROOT_DIR', dirname(dirname(__FILE__)));
}
include(ROOT_DIR.'/folder2/file3.php');
?>

root/folder2/file3.php

<?php
echo 'good';
?>

[edited by: Alcoholico at 10:58 pm (utc) on Dec. 20, 2008]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved