RE: [PHP] Problem occurs when included file includes anther file.
| From: | cyz | Date: | Fri, 08 Aug 2003 09:16:21 +0000 |
| Subject: | RE: [PHP] Problem occurs when included file includes anther file. | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-7106@lists.php.net to get a copy of this message | ||
Thanks a lot.Now I understand what the problem is:)
Althought we can easily change to code to make it work,like what you
said,using full path to include scripts,I think a better solution is to
change the include() behavior in the future version of php.It shoudn't
be difficult:)
Thanks again for your help:)
-----Original Message-----
From: Christophe Chisogne [mailto:christophe@publicityweb.com]
Sent: Friday, August 08, 2003 4:38 PM
To: Nicholas Robinson
Cc: php-general@lists.php.net
Subject: Re: [PHP] Problem occurs when included file includes anther
file.
nc01.php from demo.php: ok in root/./inc01.php
Indeed, the PHP doc should be updated on that point.
I do agree with you, after struggling a while for the
same kind of problem :-)
But I've got all explanations below.
Nicholas Robinson wrote:
>>The directory structure is as follows:
>>|-- demo.php
>>|-- inc/
>> |-- inc1.php
>> |-- inc2.php
>>
>>=== file demo.php ==
>>require 'inc/inc1.php';
>>
>>=== file inc/inc1.php ==
>>require 'inc2.php';
>>
>>== file inc/inc2.php ==
>>this is a test
>>
>>The above inc/inc1.php is ok for testing.And this would be ok too:
>><?php
>>//file inc/inc1.php
>>require 'inc/inc2.php';
>>?>
>>
>>Problem occurs when the code is like this
>><?php
>>//file inc/inc1.php
>>require './inc2.php';
>>?>
>>Fatal error: main(): Failed opening required './inc2.php'
>>(include_path='.:/usr/local/lib/php')
Your code is not correct, in PHP before 4.1 at least.
The include path is always the one of the (initial) script.
It's "/" in your case. You must use "require inc/inc2.php"
even in inc1.php because the search path in inc1.php
(used by require inc/inc2.php) is '/' (initial script) and not
'/inc'.
NB this behaviour changed after 4.1. Now, the search path is
first the (initial) script, and _if_not_found_there, the path
of the including (parent, included) script.
So your code will somewhat works after PHP 4.1 (unless an included
file has the same name in the including script), but it can't
work before PHP4.1. The best option seems to always use the old
convention, to avoid confusion (and clash in file names, which
could be a difficult to find bug).
>>It seems that there's noxplanation about this in php manual.Could
>>someone give some?
Yes, but I think it's really well hidden (in doc users comments)
Look at the comment of "ivo at i7 dot nl" on "26-Nov-2002 08:56" in
http://www.php.net/manual/en/function.include.php
>>Btw,I am using redhat 7.2 / Apache 2.0.47 / PHP 4.3.3RC1
I guess your code works on that particular server and
not on another one ?
I had the same surprise that you have, so I do always this now.
1. Use old convention (require './include/inc01.php')
even in included files
2. Use '.' in front of path as show above, just in case (bad php.ini)
In fact there is a more subtle pblm left. The old semantic means
the 'require' in an included file depend on the 'require' of the
including file! Here under an example to better understand.
It happens when you have a subdir (say admin) which contain php
libraries used in that subdir and in the parent dir (can be often)
I suppose an 'old' PHP (before 4.1) as some of the ones I use.
Path:
root/demo.php
root/inc/inc01.php
root/admin/demoadm.php
root/admin/inc/lib01.php
root/admin/inc/lib02.php
Includes:
lib01.php incl lib02.php : require './inc/lib02.php'
demoadm.php incl lib01.php: require './inc/lib01.php'
demo.php incl inc01.php: require './inc/inc01.php'
Ok until now for demo.php and demoadm.php, but add this include:
inc01.php incl lib01.php: require './admin/inc/lib01.php'
Code is broken in demo.php but not for demoadm.php
When calling demoadm.php, search path is root/admin,
includes:
./inc/lib01.php from demoadm.php: ok in root/admin/./inc/lib01.php
./inc/lib02.php from lib01.php: ok in root/admin/./inc/lib02.php
when calling demo.php, search path is root/
includes:
./inc01.php from demo.php: ok in root/./inc01.php
./admin/inc/lib01.php from inc01.php: ok in root/./admin/inc/lib01.php
./inc/lib02.php from lib01.php: KO in root/./inc/lib02.php
Pblm is in lib01.php:
- called from demoadm, we need './inc/lib02.php'
- called from demo, we need './admin/inc/lib02.php'
I guess that's the reason of the change in include/require semantics.
But it can be resolved simply. Here's my solution
1. create root/admin/inc/admconfig.php (all cfg vars for root/admin/)
2. in admconfig.php, set admindir var: " $admindir= './' "
3. create root/inc/config.php (all cfg vars for root/)
4. in config.php, set admindir var: " $admindir= './admin/' "
5. in 'lib01-type' files, use
require $admindir . './inc/lib02.php'
6. of course, include admconfig.php in all root/admin/ php files
and config.php in all root/ php files
Another solutions left as exercice, as well as asking PHP guys
to update the doc for includes ;-)
--
Christophe Chisogne
Developper, Publicityweb sprl
http://www.publicityweb.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php