Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        good night

        nice post

        _______________
        imran

        Comment

        Previously entered content was automatically saved. Restore or Discard.
        Auto-Saved
        x
        Insert: Thumbnail Small Medium Large Fullsize Remove  
        x
        x
        Working...
        X