weird include() problem – HELP my library is #?@*ed!! – PHP

  php

Q(Question):

Hey,

I’ve written a custom HTML library using many PHP scripts as seperate
files (just like I’d do a java project) and I’m having some problems
where I’m including scripts in different directories from already
included scripts… basically where there’a an include chain spanning
multiple directories.

I’ve reduced the problem to its core:

Imagine you have a website consisting of 4 PHP files:

/top_site.php
/inlcudes/top_lib.php
/includes/lib/lib1/lib1.php
/includes/lib/lib2/lib2.php

the contents of which are:

top_site.php = <? include_once("includes/top_lib.php"); ?>
top_lib.php = <? include_once("lib/lib1/lib1.php"); ?>
lib1.php = <? include_once("../lib2/lib2.php"); ?>
lib2.php = <? echo "Hello World!"; ?>

now when I go to http://YOUR_WEBSITE_ROOT/top_site.php I get an error
saying that the include within lib1.php can’t find lib2.php…

….BUT if I go directly to
http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php then you’ll see that
it includes lib2.php fine!!!

Try it for yourself; it won’t take a minute to set up.

I’ve checked my include path and it looks fine (having ‘.’ in it), so
I’m basically stuck there. Oh, and I’m running PHP version 4.2.2 on
Linux RH9/Apache 2.

Has anyone else seen this before? Have any ideas? Cos I don’t really
want to have to convert everything over to absolute (not sure if
this’ll work either) and I’ve written LOTS of code! D’OH!

Thanks,
Rob Long.

A(Answer):

"Rob Long" <bo******@gmx.net> wrote in message news:95*************************@posting.google.co m…

Hey,

I’ve written a custom HTML library using many PHP scripts as seperate
files (just like I’d do a java project) and I’m having some problems
where I’m including scripts in different directories from already
included scripts… basically where there’a an include chain spanning
multiple directories.

I’ve reduced the problem to its core:

Imagine you have a website consisting of 4 PHP files:

/top_site.php
/inlcudes/top_lib.php
/includes/lib/lib1/lib1.php
/includes/lib/lib2/lib2.php

the contents of which are:

top_site.php = <? include_once("includes/top_lib.php"); ?>
top_lib.php = <? include_once("lib/lib1/lib1.php"); ?>
lib1.php = <? include_once("../lib2/lib2.php"); ?>
lib2.php = <? echo "Hello World!"; ?>

now when I go to http://YOUR_WEBSITE_ROOT/top_site.php I get an error
saying that the include within lib1.php can’t find lib2.php…

…BUT if I go directly to
http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php then you’ll see that
it includes lib2.php fine!!!

Try it for yourself; it won’t take a minute to set up.

Hi Rob,

The includes are relative to the web address – not relative to the code.

So http://YOUR_WEBSITE_ROOT/top_site.php is looking for /webroot/../lib2/lib2.php – which doesn’t exist

and http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php is looking for
/webroot/includes/lib1/../lib2/lib2.php – which does exist.
What you want is something like:

require_once ($_SERVER[‘DOCUMENT_ROOT’]."/includes/lib2/lib2.php");
The way I use the includes is like this:

Imagine your directory as follows

/index.php
/main.php
/inc/menu.php
/content1/index.php
/content1/main.php
/content2/index.php
/content2/main.php

File /index.php:
require_once ($_SERVER[‘DOCUMENT_ROOT’]."/inc/menu.php");
require_once ("main.php");

File /inc/menu.php
Option 1 Option 2 Option 3<br>

Files /content1/index.php and /content2/index.php:
require_once ($_SERVER[‘DOCUMENT_ROOT’]."/index.php");

/main.php
Welcome to LocalHost!

/content1/main.php
Welcome to Content 1!

/content2/main.php
Welcome to Content 2!
What does all of this give you?

http://localhost/
Option 1 Option 2 Option 3
Welcome to LocalHost!

http://localhost/content1/
Option 1 Option 2 Option 3
Welcome to Content 1!

http://localhost/content2/
Option 1 Option 2 Option 3
Welcome to Content 2!
Now updating Menu updates the menu for all pages on the site. Same could be done for headers, footers, etc.
-CF

A(Answer):

Rob Long wrote:

Cos I don’t really want to have to convert everything over to absolute

Try

<?php
set_include_path(get_include_path() .
‘:/:/includes:/includes/lib/lib1:/includes/lib/lib2’);
?>

to have php use all those directories as the source for the include.

–= my mail box only accepts =–
–= Content-Type: text/plain =–
–= Size below 10001 bytes =–

A(Answer):

"ChronoFish" <de**@chronofish.com> wrote in message news:<kwZKb.24850$ti2.13240@lakeread03>…

"Rob Long" <bo******@gmx.net> wrote in message news:95*************************@posting.google.co m…
What you want is something like:

require_once ($_SERVER[‘DOCUMENT_ROOT’]."/includes/lib2/lib2.php");

Thanks a lot mate. Shame that the PHP parser isn’t smart enough to
have some kind of include counter, writing all the includes to the
output script at the end, therefore allowing include paths to be
relative from the SCRIPT IN WHICH THEY ARE DECLARED! Which makes a lot
more sense to me. You wouldn’t have any problems taking a library as a
package and moving it from project to project etc.

Sigh.

Rob.

A(Answer):

Rob Long wrote:

Thanks a lot mate. Shame that the PHP parser isn’t smart enough to
have some kind of include counter, writing all the includes to the
output script at the end, therefore allowing include paths to be
relative from the SCRIPT IN WHICH THEY ARE DECLARED! Which makes a lot
more sense to me. You wouldn’t have any problems taking a library as a
package and moving it from project to project etc.

Try this

#v+
<?php
$rel1 = ‘../../include.php’;
$rel2 = ‘forward/path/include.php’;

include preg_replace(‘@^(.*/)[^/]+$@’, ‘$1’, __FILE__) . $rel1;
include preg_replace(‘@^(.*/)[^/]+$@’, ‘$1’, __FILE__) . $rel2;
?>
#v-

Happy Coding 🙂

–= my mail box only accepts =–
–= Content-Type: text/plain =–
–= Size below 10001 bytes =–

A(Answer):

I noticed that Message-ID: <kwZKb.24850$ti2.13240@lakeread03> from
ChronoFish contained the following:

Try it for yourself; it won’t take a minute to set up.

Hi Rob,

The includes are relative to the web address – not relative to the code.

How come I have a file in a subdirectory which has

include ‘1.php’;

which includes file in that subdirectory just fine.


Geoff Berrow (put thecat out to email)
It’s only Usenet, no one dies.
My opinions, not the committee’s, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

A(Answer):

"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message news:sv********************************@4ax.com…

I noticed that Message-ID: <kwZKb.24850$ti2.13240@lakeread03> from
ChronoFish contained the following:

The includes are relative to the web address – not relative to the code.

How come I have a file in a subdirectory which has

include ‘1.php’;

which includes file in that subdirectory just fine.

Don’t know. If your search path includes the subdirectory, or if the subdirectory is the directory you’re looking at it would work.
Otherwise it shouldn’t.

-CF

LEAVE A COMMENT