w3it.org
+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    bearfrods Guest

    Default How to get a PHP file to be included across files in different directories?

    Currently I have created some php based webpages and uploaded them to my web hosting server.It has a PHP 5.1.6
    installed.With this I am able to include files only that are in the same directory(they work well using require() fn).If not an error message displaying some "no such file" in directory appears in the page.I would like to use them to be included in different direcetories.What should I do?

  2. #2
    Join Date
    Mar 2007
    Location
    Italy
    Posts
    1,583

    Default include or require?

    Hello, you can use, both the two instructions, include or require in php, they to the same, but in different way.
    You should use require_once in php, when the included file should be included, by the php parser engine, only one time while a script or program is parsed.
    Use the php include instruction, as in your case, in any others situations (html files maybe).
    You can see many easy examples at php.net php online manual for the include/require that are very well explained:
    http://php.net/manual/en/function.include.php
    by the way if you like to include files that resides in differents directories, you can use, for example code like this (the include using relative path):
    PHP Code:
    <?php
    include('./dir/otherDir/index.php');
    ?>
    The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.

  3. #3
    Join Date
    Mar 2007
    Location
    Italy
    Posts
    1,583

    Default include/require example

    p.s in the php online manual, this maybe help you (i have simplified the php.net example):
    PHP Code:
    <?php
    if(!include('./themes/skin/index.php'))
    {
      
    // file was missing so include default theme
      
    require('./themes/default_theme/index.php');
    }
    ?>

    Or maybe this works better

    <?php
    if(file_exsists('./themes/skin/index.php'))
       include(
    './themes/skin/index.php');
    else
       require(
    './themes/default_theme/index.php');
    ?>
    p.s if any more helpn needed just let know

Tags for this Thread

Posting Permissions

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