472,958 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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

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.
Jul 17 '05 #1
6 2653

"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
Jul 17 '05 #2
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 =--
Jul 17 '05 #3
"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.
Jul 17 '05 #4
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 =--
Jul 17 '05 #5
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/
Jul 17 '05 #6

"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
Jul 17 '05 #7

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

Similar topics

2
by: Kupo | last post by:
Hi, I'm currently writing website using php. My problem is, when I do: include("../../library/file.php"); // this is from e.g /level1/level2/something.php it works. However, when I use: ...
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
1
by: Jim Bancroft | last post by:
Hi, I've used VC++ 6 for my previous C++ work and am having a little trouble acclimating to VS .Net 2003, mainly in establishing where to look for additional header and library files. I'm...
3
by: jchimanzi | last post by:
I am trying to develop a small program which does library books. I have tried the code below but it still does not work. Can someone help me to debug the program and advice accordingly. ii) //...
4
by: jchimanzi | last post by:
Please can someone help in correcting the following attempt of the questions below. I have included my attempt at the problem I have been asked to write class definations which does the following...
5
by: thegreatgonzo | last post by:
I have a little problem. Read this please: //library.h class myClass { public: myClass(); static myClass *instance(); private: static myClass *self;
1
by: Łukasz Z±bik | last post by:
I have project that uses managed c++ where I use c - library, this library contains some variables named generic, during compilation I get error: Error 1 error C2146: syntax error : missing ';'...
4
by: Travis | last post by:
I'm not a Makefile expert. I have a real small program (just a main.cpp) that needs to tie into an existing library. Now other programs I've seen that use the library just do #include "Library.h"...
2
by: dgpnich | last post by:
I am using msaccess with db2 but this seems to apply to any odbc linked table. I do not want the libary to be included in the link name example MISCMFIL_NAMMSP where MISCMFIL is the libray and...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.