472,145 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

__FILE__ vs. $_SERVER['DOCUMENT_ROOT'] ?

Tom
I'm trying to dynamically adjust the path for one of my scripts using
this:

$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,

Tom

Jul 17 '05 #1
6 8267
"Tom" <kl******@gmail.com> wrote in news:1107681034.673493.296040
@f14g2000cwb.googlegroups.com:
I'm trying to dynamically adjust the path for one of my scripts using
this:

$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?
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.
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)?


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.

hth
--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources
Jul 17 '05 #2
Tom
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
root.

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

Jul 17 '05 #3
DH
Tom wrote:
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
root.

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


MySQL runs on PC's development environment as 127.0.0.1 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.

switch($_SERVER['HTTP_HOST'])
{
Case '127.0.0.1':
error_reporting(E_ALL);
$opts['forms_path'] =
'c:\\apache\\htdocs\\domain_name\\public_html\\inc \\';
$opts['log_path'] =
'c:\\apache\\htdocs\\domain_name\\public_html\\inc 2\\';
$opts['redirect_url'] =
$_SERVER['HTTP_HOST'].'/domain_name/public_html/';
break;
default:
error_reporting(0);
$opts['forms_path'] = '/home/username/public_html/inc/';
$opts['log_path'] = '/home/username/public_html/inc2/';
$opts['redirect_url'] = $_SERVER['HTTP_HOST'];
break;
};
Jul 17 '05 #4
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
application.

hth
r.

Tom wrote:
I'm trying to dynamically adjust the path for one of my scripts using
this:

$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,

Tom


Jul 17 '05 #5

"Tom" <kl******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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
root.

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


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)) ?
$_SERVER['DOCUMENT_ROOT'] : (dirname($_SERVER["SCRIPT_NAME"]) ==
DIRECTORY_SEPARATOR) ? "." : str_repeat("../",
substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/"))) ;

Mike
--
Mike Walsh - mike_walsh at mindspring.com
Jul 17 '05 #6
....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?

r.

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Chris | last post: by
2 posts views Thread by George Zervakos | last post: by
4 posts views Thread by Ronald Raygun | last post: by
6 posts views Thread by Jeff | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.