Problem occurs when included file includes anther file.

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Problem occurs when included file includes anther file.


    The directory structure is as follows:
    /
    |-- demo.php
    |-- inc/
    |-- inc1.php
    |-- inc2.php

    === file demo.php ==
    <?php
    require 'inc/inc1.php';
    ?>

    === file inc/inc1.php ==
    <?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')

    It seems that there's no explanation about this in php manual.Could
    someone give some?

    Btw,I am using redhat 7.2 / Apache 2.0.47 / PHP 4.3.3RC1 as my testing
    server.
    Thank you for reading my post:)


    Caiyongzhou Guest

  2. Similar Questions and Discussions

    1. Looking for a simple .w3d file with bones included
      Hi, I would like to test #BONESPLAYER , but I have no .w3d files with bones. I plan on buying shockwave avatars from www.geo-metricks.com...
    2. BIG file sizes with ICC included
      I just noticed this after fixing up my color settings and icc profile. When i save an AI CS file, and have "include ICC profile" checked in the...
    3. [PHP] Problem occurs when included file includes anther file.
      Thank you for your reply first. Yes I know I could set include_path either in the php.ini or at runtime.But it's a little bit dirty I think.What...
    4. Check if php file is an included file?
      If have file1.php that includes file2.php, is there any way to check, from file2.php, if it is indeed included. In other words, I don't want...
    5. [PHP] Check if php file is an included file?
      On Tuesday 15 July 2003 15:25, John Manko wrote: get_included_files() -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source...
  3. #2

    Default Re: [PHP] Problem occurs when included file includes anther file.

    ooops, sorry, ignore my last post, I must get larger text on my screen! Missed
    the .: element of your path!

    Can you confirm that this file is a replacement for demo.php?

    "Problem occurs when the code is like this
    <?php
    //file inc/inc1.php
    require './inc2.php';
    ?>"

    If so, then it won't find it because you haven't include ./inc in your include
    path.

    Otherwise your example works fine on my machine.

    HTH

    Nick

    On Friday 08 Aug 2003 6:38 am, CaiYongzhou wrote:
    > The directory structure is as follows:
    > /
    >
    > |-- demo.php
    > |-- inc/
    > |
    > |-- inc1.php
    > |-- inc2.php
    >
    > === file demo.php ==
    > <?php
    > require 'inc/inc1.php';
    > ?>
    >
    > === file inc/inc1.php ==
    > <?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')
    >
    > It seems that there's noxplanation about this in php manual.Could
    > someone give some?
    >
    > Btw,I am using redhat 7.2 / Apache 2.0.47 / PHP 4.3.3RC1 as my testing
    > server.
    > Thank you for reading my post:)
    Nicholas Robinson Guest

  4. #3

    Default RE: [PHP] Problem occurs when included file includes anther file.

    Well,if so,why <?php require 'inc2.php'; ?> works?
    I didn't change the default include_path.Sure that '/inc' isn't in the
    include_path.But it just works.

    You see,the include_path ".:/usr/local/lib/php" refers to the current
    dirctory where the file locates and the PEAR directory.The focus is what
    this current directory "." refers to when the demo.php includes another
    file inc/inc1.php -- well,to demo.php, without question ,it refers to
    where the demo.php locates,but to inc/inc1.php what this "." refers to
    ? '/' or '/inc'? It seems that both are ok except in the case we use
    './'.

    -----Original Message-----
    From: Nicholas Robinson [mailto:npr@bottlehall.freeserve.co.uk]
    Sent: Friday, August 08, 2003 3:15 PM
    To: CaiYongzhou; Php General
    Subject: Re: [PHP] Problem occurs when included file includes anther
    file.

    ooops, sorry, ignore my last post, I must get larger text on my screen!
    Missed
    the .: element of your path!

    Can you confirm that this file is a replacement for demo.php?

    "Problem occurs when the code is like this
    <?php
    //file inc/inc1.php
    require './inc2.php';
    ?>"

    If so, then it won't find it because you haven't include ./inc in your
    include
    path.

    Otherwise your example works fine on my machine.

    HTH

    Nick

    On Friday 08 Aug 2003 6:38 am, CaiYongzhou wrote:
    > The directory structure is as follows:
    > /
    >
    > |-- demo.php
    > |-- inc/
    > |
    > |-- inc1.php
    > |-- inc2.php
    >
    > === file demo.php ==
    > <?php
    > require 'inc/inc1.php';
    > ?>
    >
    > === file inc/inc1.php ==
    > <?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')
    >
    > It seems that there's noxplanation about this in php manual.Could
    > someone give some?
    >
    > Btw,I am using redhat 7.2 / Apache 2.0.47 / PHP 4.3.3RC1 as my testing
    > server.
    > Thank you for reading my post:)



    Cyz Guest

  5. #4

    Default 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
    [url]http://www.php.net/manual/en/function.include.php[/url]
    >>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
    [url]http://www.publicityweb.com[/url]

    Christophe Chisogne Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139