php include files in subdirectories

Discussion in 'PHP' started by donteatthat, Jun 15, 2010.

  1. #1
    Hi, sorry new to programming.

    I am using include files as so in every page i.e. /index.php

    <?php include("/includes/simon.php"); ?>

    at root level this include file works fine,

    With any sub folder i.e. /subfolder/index.php it comes up with an error,

    Warning: main(/includes/simon.php): failed to open stream: No such file or directory in etc etc \subfolder\index.php on line 15

    I thought putting the inital forward slash "/" would work? any quick fix other than putting a "../" in all the subfolder files?

    Many thanks for reading,

    Simon
     
    donteatthat, Jun 15, 2010 IP
  2. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    <?php include("includes/simon.php"); ?> will works...!, if you are in forder x, the includes folder must be with in x,
     
    strgraphics, Jun 15, 2010 IP
  3. netload

    netload Member

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Another solution - absolute path via $_SERVER['DOCUMENT_ROOT']

    <?php
    define('APPLICATION_PATH', $_SERVER['DOCUMENT_ROOT']);
    include( APPLICATION_PATH."/includes/simon.php");
    ?>

    Don't use $_SERVER['DOCUMENT_ROOT'] direct in the include(), some day it can make troubles.
     
    netload, Jun 15, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    ^^ yah or include( dirname(__FILE__) . '/subdir/simon.php' );
     
    gapz101, Jun 15, 2010 IP
  5. amaroks

    amaroks Peon

    Messages:
    242
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    All The Above should work :) and there are some more ways
     
    amaroks, Jun 15, 2010 IP
  6. Bell4306

    Bell4306 Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The Best Fixes have already been Posted!
     
    Bell4306, Jun 15, 2010 IP
  7. ZachF

    ZachF Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Depends on your php settings i believe, but you can even include php files on other sites.

    Anyways, just do the full path to the file. Like

    include "/home/username/public_html/folder/file.php";
     
    ZachF, Jun 15, 2010 IP