Connecting Tech Pros Worldwide Forums | Help | Site Map

Relative paths in require_once problem (possibly all includeroutines)

Royan
Guest
 
Posts: n/a
#1: Dec 16 '07
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.require]: Failed opening
required '../../global.inc' (include_path='.;E:\www\root\') in E:\www
\root\utils\logs\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(dirname(__FILE__)."/../../global.inc");
Steve
Guest
 
Posts: n/a
#2: Dec 16 '07

re: Relative paths in require_once problem (possibly all includeroutines)



"Royan" <romayankin@gmail.comwrote in message
news:8769e733-17fe-4083-865c-ccd496707023@e25g2000prg.googlegroups.com...
Quote:
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.require]: Failed opening
required '../../global.inc' (include_path='.;E:\www\root\') in E:\www
\root\utils\logs\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.php, 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.php:

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

hth.


Royan
Guest
 
Posts: n/a
#3: Dec 17 '07

re: Relative paths in require_once problem (possibly all includeroutines)


Thanks Steve, thats a great idea, i've especially liked that part :)
Quote:
Quote:
>$relativePath = strlen($parsedUri) - strlen($relativeUri) - 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
Quote:
Quote:
>>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:
Quote:
"Royan" <romayan...@gmail.comwrote in message
>
news:8769e733-17fe-4083-865c-ccd496707023@e25g2000prg.googlegroups.com...
>
>
>
Quote:
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:
>
Quote:
/root/global.inc
|__/files/foo.php
|__/utils
|__/logs/logger.inc
>
Quote:
When I run foo.php I get the following error:
==========
Fatal error: require_once() [function.require]: Failed opening
required '../../global.inc' (include_path='.;E:\www\root\') in E:\www
\root\utils\logs\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"
>
Quote:
See, foo.php includes its file as "../global.inc" and logger.inc
"../../global.inc" (note relative path differs)
>
Quote:
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.
>
Quote:
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.php, 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.php:
>
<?
$parsedUri = dirname($_SERVER['PHP_SELF']);
$parsedUri .= substr($parsedUri, -1) != '/' ? '/' : '';
$relativeUri = str_replace('/', '', $parsedUri);
$relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
if ($relativePath < 0){ $relativePath = 0; }
$relativePath = str_repeat('../', $relativePath);
if (!$relativePath){ $relativePath = './'; }
?>
>
hth.
Steve
Guest
 
Posts: n/a
#4: Dec 17 '07

re: Relative paths in require_once problem (possibly all includeroutines)



"Royan" <romayankin@gmail.comwrote in message
news:a9f101e8-d722-419e-b54c-e6499e1b943a@e23g2000prf.googlegroups.com...
Quote:
Thanks Steve, thats a great idea, i've especially liked that part :)
Quote:
Quote:
>>$relativePath = strlen($parsedUri) - strlen($relativeUri) - 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.


Royan
Guest
 
Posts: n/a
#5: Dec 17 '07

re: Relative paths in require_once problem (possibly all includeroutines)


On Dec 17, 5:14 pm, "Steve" <no....@example.comwrote:
Quote:
"Royan" <romayan...@gmail.comwrote in message
>
news:a9f101e8-d722-419e-b54c-e6499e1b943a@e23g2000prf.googlegroups.com...
>
Quote:
Thanks Steve, thats a great idea, i've especially liked that part :)
Quote:
>$relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
>
Quote:
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 :)
Steve
Guest
 
Posts: n/a
#6: Dec 17 '07

re: Relative paths in require_once problem (possibly all includeroutines)



"Royan" <romayankin@gmail.comwrote in message
news:b0373a93-ff5f-4cfb-9f4e-074e9760550c@a35g2000prf.googlegroups.com...
Quote:
On Dec 17, 5:14 pm, "Steve" <no....@example.comwrote:
Quote:
>"Royan" <romayan...@gmail.comwrote in message
>>
>news:a9f101e8-d722-419e-b54c-e6499e1b943a@e23g2000prf.googlegroups.com...
>>
Quote:
Thanks Steve, thats a great idea, i've especially liked that part :)
>>$relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
>>
Quote:
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.


Porfirio
Guest
 
Posts: n/a
#7: Dec 27 '07

re: Relative paths in require_once problem (possibly all includeroutines)


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??
Closed Thread