473,652 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bad php behaviour with related paths


Hello all

I'm trying to include some files in an included file. Think of some scripts
like this

/index.php
/scripts/logging.php
/scripts/config.php
/scripts/db_access.php

index.php includes 'scripts/logging.php' and logging.php includes
'config.php' ºi 'db_access.php' .

My problem is that from within logging.php the php will try to include the
file '/config.php' and not '/scripts/config.php' because logging.php is
included in '/index.php' in the first place.

I would use absolute paths in the included file names, built with
$_SERVER['DOCUMENT_ROOT'], but I don't really know where my pages are on
the web server. I just know they are somewhere under DocumentRoot.

How can I write pages that can translate local paths to virtual paths and
that can be moved from one place to another in the virtual directory
hierarhy and still function corectly ?

How can I write pages that know about each other's URI's and that do not
care where they are placed on the webserver with respect to DocumentRoot ?
--
Thank you
Timothy Madden
Romania
------------------------------------
And I don't wanna miss a thing
Jul 17 '05 #1
7 2006
"Timothy Madden" wrote:
Hello all

I’m trying to include some files in an included file. Think of
some scripts
like this

/index.php
/scripts/logging.php
/scripts/config.php
/scripts/db_access.php

index.php includes ’scripts/logging.php’ and logging.php
includes
’config.php’ ºi ’db_access.php’ .

My problem is that from within logging.php the php will try to include the
file ’/config.php’ and not
’/scripts/config.php’ because logging.php is
included in ’/index.php’ in the first place.

I would use absolute paths in the included file names, built with
$_SERVER[’DOCUMENT_ROOT’], but I don’t really know
where my pages are on
the web server. I just know they are somewhere under DocumentRoot.

How can I write pages that can translate local paths to virtual paths and
that can be moved from one place to another in the virtual directory hierarhy and still function corectly ?

How can I write pages that know about each other’s URI’s
and that do not
care where they are placed on the webserver with respect to
DocumentRoot ?


Timothy, you don’t have to worry about where the files are on the
server. All you have to worry about is correctly setting up the paths
relative to the TOP calling script (in your case, where index.php
resides).

If you do it this way it will work:
in index.php ... include("./scripts/logging.php")
in logging.php ... include("./scripts/config.php")

You cannot say in logging.php ... include("config .php") since php
will look at the root to resolve this.

Hope this helps. This is one of the IMHO "weaknesses " of php, and
your situation comes up every day on usenet, so you are not alone.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Bad-beha...ict136912.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=457260
Jul 17 '05 #2
Why don't you put 'scripts' in the include_path in your php.ini file? That
is what it is there for.

--
Tony Marston

http://www.tonymarston.net
"Timothy Madden" <ba****@rmv.spa m.home.ro> wrote in message
news:uz******** *************** *****@40tude.ne t...

Hello all

I'm trying to include some files in an included file. Think of some scripts like this

/index.php
/scripts/logging.php
/scripts/config.php
/scripts/db_access.php

index.php includes 'scripts/logging.php' and logging.php includes
'config.php' ºi 'db_access.php' .

My problem is that from within logging.php the php will try to include the
file '/config.php' and not '/scripts/config.php' because logging.php is
included in '/index.php' in the first place.

I would use absolute paths in the included file names, built with
$_SERVER['DOCUMENT_ROOT'], but I don't really know where my pages are on
the web server. I just know they are somewhere under DocumentRoot.

How can I write pages that can translate local paths to virtual paths and
that can be moved from one place to another in the virtual directory
hierarhy and still function corectly ?

How can I write pages that know about each other's URI's and that do not
care where they are placed on the webserver with respect to DocumentRoot ?
--
Thank you
Timothy Madden
Romania
------------------------------------
And I don't wanna miss a thing

Jul 17 '05 #3
Timothy Madden <ba****@rmv.spa m.home.ro> wrote or quoted:
I'm trying to include some files in an included file. Think of some scripts
like this

/index.php
/scripts/logging.php
/scripts/config.php
/scripts/db_access.php

index.php includes 'scripts/logging.php' and logging.php includes
'config.php' ?i 'db_access.php' .

My problem is that from within logging.php the php will try to include the
file '/config.php' and not '/scripts/config.php' because logging.php is
included in '/index.php' in the first place.

I would use absolute paths in the included file names, built with
$_SERVER['DOCUMENT_ROOT'], but I don't really know where my pages are on
the web server. I just know they are somewhere under DocumentRoot.

How can I write pages that can translate local paths to virtual paths and
that can be moved from one place to another in the virtual directory
hierarhy and still function corectly ?

How can I write pages that know about each other's URI's and that do not
care where they are placed on the webserver with respect to DocumentRoot ?


You /could/ use this code:

<?php
function include_once_re lative($path) {
$from = realpath(dirnam e($_SERVER["SCRIPT_FILENAM E"]));
$to = realpath(dirnam e(__FILE__)."/".$path);

include_once(re lative_path($to ,$from));
}

function relative_path ($targetfile, $basedir = '.') {
$basedir = realpath ($basedir);
$targetfile = realpath ($targetfile);

// on windows, check that both paths are on the same drive
if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
return false;
}

// split each path into its directories
$base_parts = split ('\/', str_replace ('\\', '/', $basedir));
$target_parts = split ('\/', str_replace ('\\', '/', $targetfile));

// ensure that there are no empty elements at the end (c:\ would cause it)
for ($i = count($base_par ts) - 1; $i >= 0; $i--) {
if ($base_parts[$i] == '') {
unset ($base_parts[$i]);
} else {
break;
}
}
for ($i = count($target_p arts) - 1; $i >= 0; $i--) {
if ($target_parts[$i] == '') {
unset ($target_parts[$i]);
} else {
break;
}
}

// get rid of the common directories at the beginning of the paths
$common_count = 0;
for ($i = 0; $i < count($base_par ts); $i++) {
if ($target_parts[$i] == $base_parts[$i]) {
$common_count++ ;
} else {
break;
}
}
for ($i = 0; $i < $common_count; $i++) {
unset ($base_parts[$i]);
unset ($target_parts[$i]);
}

// build the resulting string
$cnt = count($base_par ts) - 1;
if ($cnt < 1) {
$cnt = 0;
}

return str_repeat ('../', $cnt).implode('/', $target_parts);
}

?>

Then calling "include_once_r elative" with relative paths would include
paths relative to the library code, not relative to the document.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #4
steve <Us************ @dbforumz.com> wrote or quoted:
"Timothy Madden" wrote: Timothy, you don?t have to worry about where the files are on the
server. All you have to worry about is correctly setting up the paths
relative to the TOP calling script (in your case, where index.php
resides).
That is not a feasible solution for library code which needs
to include other library code.

It results in the caller being required to manually include all the
needed library files, instead of just one of them (which includes all
the rest).
You cannot say in logging.php ... include("config .php") since php
will look at the root to resolve this.

Hope this helps. This is one of the IMHO "weaknesses " of php, and
your situation comes up every day on usenet, so you are not alone.


It is indeed a disasterous design decision by the PHP authors :-(

There are work-arounds - but they are not neat - and PHP seems to badly
need an "include_relati ve" function built into its core.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #5
Tony Marston <to**@nospam.de mon.co.uk> wrote or quoted:
Why don't you put 'scripts' in the include_path in your php.ini file? That
is what it is there for.


Modularily, encapsulation, not messing up the global namespace,
not requing users of your code to have access to the php.ini file -
there are *lots* of good reasons for not going down that path.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #6
If you don't have access to the php.ini file there are several ways to
change the settings:
(a) By using a .htaccess file (assuming you are using Apache)
(b) By using ini_set().

If I were wring software that needed to be initiated by include(), and this
software used include() on files in subdirectories, then I would write it so
that it worked.

If enough people requested an include_relativ e() function then I'm sure the
authors of PHP would add it in. But it seems that only a small number
*think* that it's necessary while the rest of us write code that doesn't
need it.

--
Tony Marston

http://www.tonymarston.net
"Tim Tyler" <ti*@tt1lock.or g> wrote in message news:I2******** @bath.ac.uk...
Tony Marston <to**@nospam.de mon.co.uk> wrote or quoted:
Why don't you put 'scripts' in the include_path in your php.ini file?
That
is what it is there for.


Modularily, encapsulation, not messing up the global namespace,
not requing users of your code to have access to the php.ini file -
there are *lots* of good reasons for not going down that path.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.

Jul 17 '05 #7

"steve" <Us************ @dbForumz.com> wrote in message
news:41******** **@news.athenan ews.com...
"Timothy Madden" wrote:
> Hello all
>
> I'm trying to include some files in an included file. Think of
> some scripts
> like this
>
> /index.php
> /scripts/logging.php
> /scripts/config.php
> /scripts/db_access.php
>
> index.php includes 'scripts/logging.php' and logging.php
> includes
> 'config.php' ºi 'db_access.php' .
>
> My problem is that from within logging.php the php will try to

include
> the
> file ’/config.php’ and not
> ’/scripts/config.php’ because logging.php is
> included in ’/index.php’ in the first place.
>
> I would use absolute paths in the included file names, built with
> $_SERVER[’DOCUMENT_ROOT’], but I don’t really know
> where my pages are on
> the web server. I just know they are somewhere under DocumentRoot.
>
> How can I write pages that can translate local paths to virtual

paths
> and
> that can be moved from one place to another in the virtual

directory
> hierarhy and still function corectly ?
>
> How can I write pages that know about each other’s URI’s
> and that do not
> care where they are placed on the webserver with respect to
> DocumentRoot ?
>
>


Timothy, you don't have to worry about where the files are on the
server. All you have to worry about is correctly setting up the paths
relative to the TOP calling script (in your case, where index.php
resides).

If you do it this way it will work:
in index.php ... include("./scripts/logging.php")
in logging.php ... include("./scripts/config.php")

You cannot say in logging.php ... include("config .php") since php
will look at the root to resolve this.

Hope this helps. This is one of the IMHO "weaknesses " of php, and
your situation comes up every day on usenet, so you are not alone.

After a few moths ...
Time has passed, PHP 5 appeared, docs got a little better and now the
answer camed:

There is a constant named __FILE__ that is the name of the current
(included) file. With it I can get the base directory and compose the path
to other included files.

And __FILE__ was there since PHP 4.0, this was just a problem of
the quality of documentation for PHP (which I find to be very good
otherwise)

Timothy Madden
Romania
---------------------------------------------
And I don't wanna miss a thing
(remove 'rmv.spam' from my mail address)
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1450
by: Simon Wittber | last post by:
Greetings fellow Python People! I am using the latest debian-unstable Apache 1.3 and mod-python 2.7 with python 2.3.3 Yesterday I wrote a small mod-python script which returned the URL entered as a XML snippet. This morning, I created a new directory, and copied this script and ..htaccess file across to a new directory, and changed the program to
3
1287
by: Brian van den Broek | last post by:
Hi all, I'm just starting to employ unit testing (I'm using doctest), and I am uncertain how to handle writing tests where the behaviour being tested is dependant on whether certain file paths point to actual files. I have a class which takes, in its __init__, a list of file paths to process. The class has a method to validate that the paths passed in are appropriate ones for the class. One portion of the validation code ensures that...
45
2769
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
0
2697
by: Ethel Aardvark | last post by:
I am running a 9.0.1 database on a W2K server and have come across some strange behaviour with a SQL query. I have a query which runs in a PL/SQL cursor which has several PL/SQL variables used to switch on and off certain rules. One idea I had was to have two queries UNIONed together with a simple switch selecting which half was to operate (I know it sounds like there are probably better ways of doing this but I have my reasons). To cut...
2
2164
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others (MSVS 7.1) do not. I can only offer two explanations for my observations: 1. The Microsoft...
4
2128
by: Wayne Aprato | last post by:
I have a simple database which was originally written in Access 97. When converted to Access 2000 file format it ran flawlessly in Access 2002. I've just tried to run it in Access 2003 and I am seeing the following behaviour: Some of the fields on the continuous main form which is a list of jobs with their related details "flicker". While this is going on the form seems to function as it should and isn't locked up. If I click and hold...
7
8624
by: Simon | last post by:
Dear reader, By running the program in MDB every think works perfect. By running the same program in MDE the VBA code in the form gives the following message: "The expression On Open you entered as the event property
285
8796
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
173
13882
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the filetype to invoke the C compiler. Here's a link http://www.microsoft.com/express/download/#webInstall The download is 2.6 megs, which is near a reasonable size for a compiler, but then setup.exe wants to download 87 megs of dot net framework...
0
8811
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
8704
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
8470
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
8590
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
7302
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...
0
5620
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
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.