php include help need

4 replies
hey any one know ho to add many diffrent files from diffrent folders in a single php file..

this code works only for same folder where footer.php place

<?php include 'footer.php';?>

i want to add different files from diffrent folder like

<?php include 'head.php';?>

<?php include '/post/mypost.php';?>

<?php include '/comment/comment.php';?>

<?php include 'footer.php';?>

so please help me..
#files #include #php
  • Profile picture of the author KirkMcD
    did you try it?
    everything you typed could work.
    {{ DiscussionBoard.errors[9542538].message }}
  • Profile picture of the author kilgore
    Check your paths. When you put a "/" in your include statement you're saying to include a file relative to the root of the server, not the document root of your website.

    Probably what you're looking for is something like:

    PHP Code:
    <?php include '../post/mypost.php';?>
    (Note the ".." before the "/" above)

    Alternatively you could do something like:
    PHP Code:
    <?php include '/path/to/DocumentRoot/post/mypost.php';?>
    {{ DiscussionBoard.errors[9543024].message }}
  • Profile picture of the author mralexanderca
    What I would recommend is first setting up a file "paths.php" that will have few constants defined to the needed paths, for example:

    // path to the root folder
    define('DIR_ROOT',dirname(__FILE__) . '/');

    // path to the config folder
    define('DIR_CONFING', DIR_ROOT . 'config');

    etc.

    include DIR_ROOT . 'footer.php';
    include DIR_ROOT . 'head.php';

    You would then include it at the top of your script and include the files as needed, this is very prone to movement of the script and you will not have to change your constants often, even if you do, you would simply do so one.

    Hope this helps.

    Mr Alexander
    {{ DiscussionBoard.errors[9548177].message }}
  • Profile picture of the author Member8200
    there's no issue on adding a single '/' before a folder name, and everything you type should work. please re-check your folder paths.

    I also suggest you use require rather than include

    require statement will produce a (E_COMPILE_ERROR) and stop the script
    include statement will only produce a warning (E_WARNING) and the script will continue

    'include' could give you more errors because the page will continue to execute even if the file you include is missing and that could affect the next line of codes


    'require' will stop the page for further execution.
    {{ DiscussionBoard.errors[9561317].message }}

Trending Topics