Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm trying to dynamically adjust the path for one of my scripts using

$script_path = str_replace( $_SERVER['DOCUMENT_ROOT'], "",
dirname(realpath(__FILE__)) ) . DIRECTORY_SEPARATOR;

The problem: the server I'm currently testing it on returns for
$_SERVER['DOCUMENT_ROOT'] something like the path "home/user/path/" and
for __FILE__ "home2/user/path/scriptdir/"  I tried to trace the
discrepancy running phpinfo, but the only reference I can find to
"home2" there is _ENV["PWD"].

Anyway, this is throwing all my best laid plans into confusion.  Can
someone help me understand why $_SERVER['DOCUMENT_ROOT'] and __FILE__
are returning different basepaths?

And how I might get my script to do what I'd like it to do (basically,
return the directory path of my script minus the document root so that
I can then append it to the absolute URL)?

Hope this makes sense.  Thanks,




Quoted text here. Click to load it

Apache allows for settings like this using vhost directives. The document
root, scriptalias directory, etc. can be set to locations on different
volumes. There's probably a symlink or two involved in this magic as
well. You could get around it using is_link() and readlink() if needed.

Quoted text here. Click to load it

Try using the superglobal $_SERVER[PHP_SELF] - that will return the path
to your script, relative to the document root. I'm not sure if it's what
you're after, but it sounds like it might be.


Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
<http://www.phplabs.com/ | PHP scripts, webmaster resources


I thank the honorable gentleman from phplabs.com for his reply.

I considered using $_SERVER[PHP_SELF], but the file I'm trying to
orient this around is an include file.  It's a config/ini file in which
I can set the file name for a login page that would be in the same
folder as this file, but I want to be able to put this folder in
different directories on different servers or relative to the document

My understanding is that I'd have to use __FILE__ as a self-referring
constant/global for a called file.

There's probably lots of other ways around this, so back to the
drawingboard.  It's a learning experience.

Any suggestions welcome.



Tom wrote:
Quoted text here. Click to load it

MySQL runs on PC's development environment as and I want no
errors displayed on the web, but all errors displayed when run locally.
And since already doing this, then I find it easier to specify paths
here for Windoze and Linux/Unix.

Case '':
    $opts['forms_path'] =
    $opts['log_path'] =
    $opts['redirect_url'] =
    $opts['forms_path'] = '/home/username/public_html/inc/';
    $opts['log_path'] = '/home/username/public_html/inc2/';
    $opts['redirect_url'] = $_SERVER['HTTP_HOST'];


Quoted text here. Click to load it

This isn't a solution but it might help you solve your problem.  It was my
solution for the problem that $_SERVER['DOCUMENT_ROOT'] doesn't exist under
IIS.  I use it to find the top (or root) of my web site and it works under
both IIS and Apache 2.0.  So if you know the file you want to reference as a
relative path from the top of your web site, you can simply append it to the
$DocumentRoot as determined by this line of PHP:

$DocumentRoot = ((key_exists('DOCUMENT_ROOT', $_SERVER)) ?
DIRECTORY_SEPARATOR) ? "." : str_repeat("../",
substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/")))  ;


Mike Walsh - mike_walsh at mindspring.com


If your host is using any symbolic links (which I suspect they are),
realpath() will resolve those symbolic links. I'm guessing this is
what's causing the discrepency. I'm just using dirname(__FILE__) to set
a constant for the script root. It seems to work well so far. The only
downside to this is that you have have the __FILE__ in the root of your


Tom wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it


....also (after working with this), if you do something like
realpath('.'), it gets you the path of the current working directory
(i.e. if you are using realpath() in an include/require that lives in a
subdirectory, you get the path to the file that contains the
include/require). Whereas, __FILE__ gets you the full path to the
current file. So if you had dirname(__FILE__) in an include/require
that lives in a subdirectory, you get the full path to the
subdirectory, not the current working directory.

Clear as mud?


Site Timeline