473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 8434
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
I'm working on a large intranet group site with a lot of pages, file types, folders, subfolders, etc. I am using the include ('../includes/file.php') , but I would like to have a single snippet...
2
by: George Zervakos | last post by:
Hello, I'm new to php and was wondering if someone could help me out with the best way to accomplish this. I have Solaris 9/Apache 1.3.31/PHP 5.1.4 that I compiled from source code and...
12
by: comp.lang.php | last post by:
Env: Windows XP, Apache 1.33, PHP 5.2.0 <? echo is_dir($_SERVER) . " for dir = " . $_SERVER); ?>
4
by: Ronald Raygun | last post by:
I have just discovered that the value reported for $_SERVER by phpinfo is different from the value I obtain when I run the following: <?php echo $_SERVER ?> My natural assumption is to assume...
6
by: Jeff | last post by:
Is $_SERVER alway available? I ask this because I've seen on windows that the perl equivalent was missing. Is this settable and accessible the same way (ie not having to use global)? Jeff
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.