Reply to SOLVED: Re: [PHP] Document root, preferred way to find it???

Your name:

Reply:


Posted by Leif Gregory on 03/07/05 07:57

Hello Tom,

Sunday, March 6, 2005, 10:00:17 PM, you wrote:
TR> Ok I see where is is going wrong, try this:

Oooooh, very close. Although you have it at $document_root and all
that needs to be added is '/include' like below:

$include = $document_root . '/include';

Otherwise it's one directory too far up because you're replacing the
site root 'test' (in this case) with 'include'.

We wanted: 'C:/Sambar/docs/test/include'

Here's from your script.

Site root:
**********

File name: C:/Sambar/docs/test/test.php
Script: /test.php
Document root: C:/Sambar/docs/test
Base: test
Include: C:/Sambar/docs/include
OS: winnt
Include: C:/Sambar/docs/include;.;C:\php5\pear

**********

Site root/folder1
**********

File name: C:/Sambar/docs/test/folder1/test.php
Script: /folder1/test.php
Document root: C:/Sambar/docs/test
Base: test
Include: C:/Sambar/docs/include
OS: winnt
Include: C:/Sambar/docs/include;.;C:\php5\pear

**********

Site root/folder1/folder2
**********

File name: C:/Sambar/docs/test/folder1/folder2/test.php
Script: /folder1/folder2/test.php
Document root: C:/Sambar/docs/test
Base: test
Include: C:/Sambar/docs/include
OS: winnt
Include: C:/Sambar/docs/include;.;C:\php5\pear

**********



After my change:

File name: C:/Sambar/docs/test/folder1/folder2/test.php
Script: /folder1/folder2/test.php
Document root: C:/Sambar/docs/test
Base: test
Include: C:/Sambar/docs/test/include
OS: winnt
Include: C:/Sambar/docs/test/include;.;C:\php5\pear

The other two folders moving back up the tree are the same result.

So the final looks like:
************************

if(isset($_SERVER["SCRIPT_FILENAME"])){
$file_name = str_replace('\\','/',$_SERVER['SCRIPT_FILENAME']);
echo "File name: $file_name<br>";

$script = str_replace('\\','/',$_SERVER['SCRIPT_NAME']);
echo "Script: $script<br>";

$document_root = str_replace($script,'',$file_name);
echo "Document root: $document_root<br>";

$base = basename($document_root);
echo "Base: $base<br>";

//$include = str_replace($base,'include',$document_root);
$include = $document_root . '/include';
echo "Include: $include<br>";

$os = strtolower(PHP_OS);
echo "OS: $os<br>";

$lk = ':';
$org_include = ini_get("include_path");
if(preg_match('/^win/i',$os)) $lk = ';';

ini_set("include_path",$include.$lk.$org_include);
echo "Include: ".ini_get("include_path")."<br>";
}

************************

It's a great effort and looks bulletproof. Can you shoot any holes in
mine? A full explanation at:
http://www.devtek.org/tutorials/dynamic_document_root.php

I only throw mine back out there because it's shorter. I think it's
solid, but only because I haven't broken it yet. :-)

Both our solutions suffer the same problem. They have to be on every
page that requires an include, because we can't rely on a new host
having our code in their auto_prepend file

**********

function dynRoot()
{
$levels = substr_count($_SERVER['PHP_SELF'],"/");

for ($i=0; $i < $levels - 1; $i++)
{
$relativeDir .= "../";
}

return $relativeDir;
}

include(dynRoot() . 'includes/somefile.php')

**********

Thanks again!


Cheers,
Leif Gregory

--
TB Lists Moderator (and fellow registered end-user)
PCWize Editor / ICQ 216395 / PGP Key ID 0x7CD4926F
Web Site <http://www.PCWize.com>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация