Reply
Taking Root... Document Root Issues
Old 01-13-2009, 01:34 PM Taking Root... Document Root Issues
Skilled Talker

Posts: 61
Name: John
Trades: 0
Problem

Multiple development sites on a single server housed in varying subfolders off the webroot, each with it's own includes directories. When using include or require with URL file-access disabled you must supply either the complete real or relative path. Example Heirarchy:

/mainsite/
/mainsite/inc
/mainsite/website1
/mainsite/website1/inc
/mainsite/website2
/mainsite/website2/inc

Possible Solution?

I've tried half a dozen solutions to mixed success from Redirects to parsing or splitting the URI to find the document root. However, when you change the folder depth and hosts as often as I do (usually moving between servers, both apache and IIS for ease of development) it wreaks havoc upon almost every solution I've tried. The closest solution I found was:

PHP Code:
$absPATH = !isset($_SERVER['DOCUMENT_ROOT']) ? str_replace'\\''/'substr($_SERVER['SCRIPT_FILENAME'],0,0-strlen($_SERVER['PHP_SELF']))).'/' $_SERVER['DOCUMENT_ROOT'] ; 
This solution only gives me the "root" folder for the server, and fails to account for depth/heirarchy changes. So I came up with this:

PHP Code:
// ### Inverse Recursive Search for "Robots.txt"
for($relPATH './'; !is_file($relPATH.'robots.txt'); $relPATH .= '../') {}
$absPATH realpath($relPATH);
// ### $relPATH = Relative path to the current site's document root.
// ### $absPATH = Absolute/Real path to the current site's document root. 
Obviously you'd want to add a little extra criteria to keep this loop from running away (If you didn't have a robots.txt file for example). While this solution is reliant upon the exisitence of a robots.txt file, this is just about the only reliable cross-platform method I could think of.

Questions

Any reasons/situations why/where this wouldn't/shouldn't be used?
Is there a better way?

Thanks in advance.

Last edited by Envision_frodo; 01-13-2009 at 01:39 PM.. Reason: Forgot to Mention
Envision_frodo is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 01-13-2009, 08:13 PM Re: Taking Root... Document Root Issues
vangogh's Avatar
Post Impressionist

Posts: 9,669
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Could you set a baseURL for each site/folder and append it to $absPATH you set. Not sure if that's the kind fo thing you're looking for. Might not be. You'd be setting it once in each main directories, but it doesn't seem to bad.
__________________
l Search Engine Friendly Web Design | Van SEO Design
l Tips On Marketing, SEO, Design, and Development | TheVanBlog
l Custom WordPress Themes
| Small Business Forum
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-14-2009, 08:10 PM Re: Taking Root... Document Root Issues
Skilled Talker

Posts: 61
Name: John
Trades: 0
Essentially I am looking for a better "dynamic" PHP equivalent to baseURL.
The basic version of what I'm looking for is:

HTML Code:
<BASE HREF="HTTP://<?= $_SERVER['HTTP_HOST'] ?>/" />
<IMG SRC="somefile.jpg" />
or

PHP Code:
$absPATH $_SERVER['DOCUMENT_ROOT']; 
include 
"$absPATH/somefile.php"
only I want to be able to move my site between servers and (the hard part) into subfolders. The idea is to make the code more dynamic allowing me to put this site anywhere and still have it work. Something like:

Code:
// $tempROOT = Variable path to the sub-site.
<BASE HREF="HTTP://<?= $_SERVER['HTTP_HOST'].$tempROOT ?>/" />
$absPATH = $_SERVER['DOCUMENT_ROOT'].$tempROOT;

<IMG SRC="somefile.jpg" />
include "$absPATH/somefile.php";
Does this make any sense or am I just dense?

I want my development sites to work given this:

http://mysite/

as well as:

http://othersite/mysite/
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Old 01-15-2009, 07:29 PM Re: Taking Root... Document Root Issues
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
Not sure if this will solve every problem, but you might try the following code to get the execution path of the current file.

PHP Code:
$path realpathdirname__FILE__ ) ); 
if you request a file located in /path/to/file.php $path will be set to "/path/to"

Hope that helps!
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions

Last edited by anderswc; 01-15-2009 at 07:30 PM..
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Reply     « Reply to Taking Root... Document Root Issues
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.17206 seconds with 13 queries