473,765 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Relative paths in require_once problem (possibly all includeroutines )

Ok the problem is quite hard to explain, but i'll try to keep it as
simple as i can. Imagine I have the following structure of my files
and folders:

/root/global.inc
|__/files/foo.php
|__/utils
|__/logs/logger.inc

When I run foo.php I get the following error:
==========
Fatal error: require_once() [function.requir e]: Failed opening
required '../../global.inc' (include_path=' .;E:\www\root\' ) in E:\www
\root\utils\log s\logger.inc on line 3
==========
That error occurs because
1) "global.inc " is included ("required") into "logger.inc " and
"foo.php"
2) "logger.inc " is included into "foo.php"

See, foo.php includes its file as "../global.inc" and logger.inc
"../../global.inc" (note relative path differs)

So if you now try to run "foo.php" the require_once from "logger.inc "
would start looking for "global.inc " relatively /root/files which is
wrong.

My question is... how do I make PHP include files relative to their
location not their current "include" directory?

PS
I know one solution but i don't like it at all. It makes use of the
following trick in each potentially included file:
/* the following will rectify the above problem when inserted to
logger.inc */
require_once(di rname(__FILE__) ."/../../global.inc");
Dec 16 '07 #1
6 2497

"Royan" <ro********@gma il.comwrote in message
news:87******** *************** ***********@e25 g2000prg.google groups.com...
Ok the problem is quite hard to explain, but i'll try to keep it as
simple as i can. Imagine I have the following structure of my files
and folders:

/root/global.inc
|__/files/foo.php
|__/utils
|__/logs/logger.inc

When I run foo.php I get the following error:
==========
Fatal error: require_once() [function.requir e]: Failed opening
required '../../global.inc' (include_path=' .;E:\www\root\' ) in E:\www
\root\utils\log s\logger.inc on line 3
==========
That error occurs because
1) "global.inc " is included ("required") into "logger.inc " and
"foo.php"
2) "logger.inc " is included into "foo.php"

See, foo.php includes its file as "../global.inc" and logger.inc
"../../global.inc" (note relative path differs)

So if you now try to run "foo.php" the require_once from "logger.inc "
would start looking for "global.inc " relatively /root/files which is
wrong.

My question is... how do I make PHP include files relative to their
location not their current "include" directory?
i know what you mean. there are other solutions but this one was a quick fix
for me and avoids some other setup/config difference on various systems.
anyway, i use the following code. if you put it into a file called
relative.path.p hp, save the file in your php.ini include_path. from then on
in all of your scripts, all you have to do is this:

<?
require_once 'relative.path. php';
require_once $relativePath . 'global.inc';
?>

put that in logger.inc and foo.php and nothing blows up. here's the code for
relative.path.p hp:

<?
$parsedUri = dirname($_SERVE R['PHP_SELF']);
$parsedUri .= substr($parsedU ri, -1) != '/' ? '/' : '';
$relativeUri = str_replace('/', '', $parsedUri);
$relativePath = strlen($parsedU ri) - strlen($relativ eUri) - 1;
if ($relativePath < 0){ $relativePath = 0; }
$relativePath = str_repeat('../', $relativePath);
if (!$relativePath ){ $relativePath = './'; }
?>

hth.
Dec 16 '07 #2
Thanks Steve, thats a great idea, i've especially liked that part :)
>$relativePat h = strlen($parsedU ri) - strlen($relativ eUri) - 1;
Unfortunately this approach works great only if you can modify PHP.ini
but when you are on virtual hosting, the only way you can modify
settings in PHP.ini is by calling ini_set() function which has to be
invoced from somewhere. And in my case this "somewhere" is global.inc
This file is meant to keep all global stuff so it has to be included
into each and every file in my project, but this is the original
problem -- i can't include it. Seems to be a vicious circle.
The only solution i can think of right now is to use the absolute path
for "global.inc " in each call to require_once. Thus i can put your
code that calculates relative path in "global.inc " and use it across
all other files.
2BKDotCom
>>logger.inc doesn't need to include global.inc as long as global.inc
has been included before logger.inc is included..
It appears I've made a mistake in my original post. In fact you don't
have to include "global.inc " into foo.php, the error would persist. If
you wish I can send you test files that replicate the problem

On Dec 17, 2:30 am, "Steve" <no....@example .comwrote:
"Royan" <romayan...@gma il.comwrote in message

news:87******** *************** ***********@e25 g2000prg.google groups.com...
Ok the problem is quite hard to explain, but i'll try to keep it as
simple as i can. Imagine I have the following structure of my files
and folders:
/root/global.inc
|__/files/foo.php
|__/utils
|__/logs/logger.inc
When I run foo.php I get the following error:
==========
Fatal error: require_once() [function.requir e]: Failed opening
required '../../global.inc' (include_path=' .;E:\www\root\' ) in E:\www
\root\utils\log s\logger.inc on line 3
==========
That error occurs because
1) "global.inc " is included ("required") into "logger.inc " and
"foo.php"
2) "logger.inc " is included into "foo.php"
See, foo.php includes its file as "../global.inc" and logger.inc
"../../global.inc" (note relative path differs)
So if you now try to run "foo.php" the require_once from "logger.inc "
would start looking for "global.inc " relatively /root/files which is
wrong.
My question is... how do I make PHP include files relative to their
location not their current "include" directory?

i know what you mean. there are other solutions but this one was a quick fix
for me and avoids some other setup/config difference on various systems.
anyway, i use the following code. if you put it into a file called
relative.path.p hp, save the file in your php.ini include_path. from then on
in all of your scripts, all you have to do is this:

<?
require_once 'relative.path. php';
require_once $relativePath . 'global.inc';
?>

put that in logger.inc and foo.php and nothing blows up. here's the code for
relative.path.p hp:

<?
$parsedUri = dirname($_SERVE R['PHP_SELF']);
$parsedUri .= substr($parsedU ri, -1) != '/' ? '/' : '';
$relativeUri = str_replace('/', '', $parsedUri);
$relativePath = strlen($parsedU ri) - strlen($relativ eUri) - 1;
if ($relativePath < 0){ $relativePath = 0; }
$relativePath = str_repeat('../', $relativePath);
if (!$relativePath ){ $relativePath = './'; }
?>

hth.
Dec 17 '07 #3

"Royan" <ro********@gma il.comwrote in message
news:a9******** *************** ***********@e23 g2000prf.google groups.com...
Thanks Steve, thats a great idea, i've especially liked that part :)
>>$relativePa th = strlen($parsedU ri) - strlen($relativ eUri) - 1;

Unfortunately this approach works great only if you can modify PHP.ini
but when you are on virtual hosting, the only way you can modify
settings in PHP.ini is by calling ini_set() function which has to be
invoced from somewhere.
no, it just involves you knowing what the path is as defined in php.ini and
having access to the path...wherein you'll drop said script. that's all.
give it a try.
Dec 17 '07 #4
On Dec 17, 5:14 pm, "Steve" <no....@example .comwrote:
"Royan" <romayan...@gma il.comwrote in message

news:a9******** *************** ***********@e23 g2000prf.google groups.com...
Thanks Steve, thats a great idea, i've especially liked that part :)
>$relativePat h = strlen($parsedU ri) - strlen($relativ eUri) - 1;
Unfortunately this approach works great only if you can modify PHP.ini
but when you are on virtual hosting, the only way you can modify
settings in PHP.ini is by calling ini_set() function which has to be
invoced from somewhere.

no, it just involves you knowing what the path is as defined in php.ini and
having access to the path...wherein you'll drop said script. that's all.
give it a try.
I've done that and it works great! The code itself is a jewel. I'd
never thought that relative path could be found by calculating
slashes :)
Dec 17 '07 #5

"Royan" <ro********@gma il.comwrote in message
news:b0******** *************** ***********@a35 g2000prf.google groups.com...
On Dec 17, 5:14 pm, "Steve" <no....@example .comwrote:
>"Royan" <romayan...@gma il.comwrote in message

news:a9******* *************** ************@e2 3g2000prf.googl egroups.com...
Thanks Steve, thats a great idea, i've especially liked that part :)
$relativePa th = strlen($parsedU ri) - strlen($relativ eUri) - 1;
Unfortunately this approach works great only if you can modify PHP.ini
but when you are on virtual hosting, the only way you can modify
settings in PHP.ini is by calling ini_set() function which has to be
invoced from somewhere.

no, it just involves you knowing what the path is as defined in php.ini
and
having access to the path...wherein you'll drop said script. that's all.
give it a try.

I've done that and it works great! The code itself is a jewel. I'd
never thought that relative path could be found by calculating
slashes :)
thanks. it certainly isn't the only solution. it just does what i need and
is less married to server config than some others.

glad it works for you.
Dec 17 '07 #6
Hi everyone!!

I am doing some work with expat for php.

lets say i have this html:

<html>
<head></head>
<body>
<foo:test somearg="foo">
<foo:other/>
</foo:test>
<bar:foo>
<b>Hello</b>
</bar:foo>
</body>
</html>
so what i want is to parse xml components with namespace, so ignore html
components

So i would like to register namespaces to search for and ones to ignore
( eg. svg: )

Maybe what i need is a html parser and not a xml parser

Can you guys point me something??
Dec 27 '07 #7

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

Similar topics

7
2779
by: Doug | last post by:
If I were to write an include with a relative path like include("../conf/config.php"); What is the use? As far as I understand it, the path is relative to the first script that is called by php. In other words, if the current working directory is /www/ and you were
7
2166
by: Dave Smithz | last post by:
Hi There, I have taken over someone else's PHP code and am quite new to PHP. I made some changes and have implemented them to a live environment fine so far. However, I now want to setup a test environment. All the PHP scripts start with a few lines of: require_once "library file at specific location on server"
5
4112
by: jason | last post by:
Can anyone help me find a solution to quickly working out relative paths to a folder in the root of my server... Although it easy when you go - say - two levels down: .../includes it becomes increasingly difficult to ball park the folder when you go deeper and wider down:
2
1855
by: Joe | last post by:
Hi, can someone tell me how to set up relativ paths in VS2003 C++ ? I have some source with a tree directory structure that segments include files in various directories: #include "../include/config.h" /* Keep this first */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/complex.h" #include "../include/su3.h"
19
5110
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other things, like php code), which resolves the correct image path when opened under / but when x.php is read into a file under /dir the image no longer resolves, for obvious reasons. With absolute paths, this isn't an issue but it is with relative...
15
6471
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would make it much easier to slap modualar blocks of markup into page frameworks, which may change and so forth. And the few extra bytes, which even for a fairly large site would not amount to as many bytes as are in a fairly small low-res image, should...
3
3239
by: Peter Wang | last post by:
Hi, all. I recently encountered a very annoying problem while using Zend Framework(ZF). We use ZF in our web application, and it works fine at the beginning, but later when concurrent requests goes high, we get very high cpu load. when i trace the httpd using strace, i find so much fstat64 syscalls, most of which failed, all these syscalls take more than 60% of cpu usage. After i check our php code carefully, i find
2
4302
by: BD | last post by:
Hi there. Using 8.2 on Windows. I have a situation where I have a db backup, which I want to deploy to a group of developer workstations. The target directory for the database files will be consistent, but the location that the backup file is on may vary. I have gone through the entire backup and restore process, and it
4
1857
chathura86
by: chathura86 | last post by:
i have created some php classes in deferent folders which requires each other i have given relative paths relative to those files i used require_once() but when i use those class in a file outside the class folder i get a error message stating that the path is incorrect i seems like path is setting relative to the page im using those classes not relative to the class which is requiring it.
0
9568
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
9404
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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
10007
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
9959
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
8833
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
7379
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
5277
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...
3
2806
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.