473,549 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__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(realpat h(__FILE__)) ) . DIRECTORY_SEPAR ATOR;

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 8451
"Tom" <kl******@gmail .com> wrote in news:1107681034 .673493.296040
@f14g2000cwb.go oglegroups.com:
I'm trying to dynamically adjust the path for one of my scripts using
this:

$script_path = str_replace( $_SERVER['DOCUMENT_ROOT'], "",
dirname(realpat h(__FILE__)) ) . DIRECTORY_SEPAR ATOR;

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@fu ng.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\\ht docs\\domain_na me\\public_html \\inc\\';
$opts['log_path'] =
'c:\\apache\\ht docs\\domain_na me\\public_html \\inc2\\';
$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(realpat h(__FILE__)) ) . DIRECTORY_SEPAR ATOR;

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******** *************@z 14g2000cwz.goog legroups.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('D OCUMENT_ROOT', $_SERVER)) ?
$_SERVER['DOCUMENT_ROOT'] : (dirname($_SERV ER["SCRIPT_NAM E"]) ==
DIRECTORY_SEPAR ATOR) ? "." : str_repeat("../",
substr_count(di rname($_SERVER["SCRIPT_NAM E"]), "/"))) ;

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
16118
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 that will work for all .php/.html pages in all subfolders. I have seen the $_SERVER indicated as a way to access the root of the website without...
2
4328
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 installed. I have multiple virtual web servers configured in httpd.conf and php scripts run only if they are placed in my first VirtualHost DocumentRoot...
12
2319
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
4126
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 that this is a joke (like most of the PHP language in my experience so far) - but seriously, does anyone know why on earth a diagnostic tool like...
6
3500
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
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7457
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.