473,804 Members | 3,732 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 8481
"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
16129
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 using ../ for each folder you have to pass up through. However, I cannot get this to work for me. ...
2
4342
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 directory
12
2338
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
4141
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 phpinfo() displays values that are different from that obtained via a script call?
6
3507
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
9706
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10580
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.