1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Include Problems From Subfolders

Discussion in 'PHP' started by The Zinni, Aug 30, 2009.

  1. #1
    I've been putting some include stuff into my sites for navigation lately. The code is working fine for all of the folders in the root directory. The subfolders and lower levels are not working properly.

    I've been adding this to the pages

    
    <?php
    include ('../include/navigation.php');
    ?>
    
    HTML:

    I have also tried this in hopes of success


    
    <?php
    include ('http://www.myurlhere.com/include/navigation.php');
    ?>
    
    HTML:

    Here are the warnings I am getting

    Warning: include(../include/navigation.php) [function.include]: failed to open stream: No such file or directory in /home/content/03/4610003/html/games/texas-holdem/index.php on line 32
    
    Warning: include() [function.include]: Failed opening '../include/navigation.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/03/4610003/html/games/texas-holdem/index.php on line 32
    HTML:
    Would appreciate any help with this.
     
    The Zinni, Aug 30, 2009 IP
  2. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what is your folder structure? what files does it fail on?
     
    Kyosys, Aug 30, 2009 IP
  3. The Zinni

    The Zinni Member

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #3
    The Zinni, Aug 30, 2009 IP
  4. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You need

    
    <?php
    
    include('.../includes/file.php');
    
    ?>
    
    PHP:
     
    almondj, Aug 30, 2009 IP
  5. mystxx

    mystxx Active Member

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    there's a solution but not the best one out there. You can try with absolute paths also ie.

    
    <?php
    
    include('/home/username/public_html/includes/file.php');
    
    ?>
    
    Code (markup):
    where ofcourse you should replace the path above with the real path.
     
    mystxx, Aug 30, 2009 IP
  6. The Zinni

    The Zinni Member

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #6
    I dont get it, thats basically what I have used only with more errors
     
    The Zinni, Aug 30, 2009 IP
  7. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #7
    shallowink, Aug 30, 2009 IP
  8. The Zinni

    The Zinni Member

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #8
    The Zinni, Aug 30, 2009 IP
  9. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #9
    For the record, using hardcoded full paths is a bad idea, it makes life hell if/when you want to move your site to a new server. The solution you chose is the right thing.

    Well, the real right thing is to dynamically calculate your script path, or have a setup script that calculates it and saves it in a global include. But this is good enough.
     
    SmallPotatoes, Aug 30, 2009 IP
  10. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Eh, I like full paths.. Just define a constant that sets up the location:

    define('DOCUMENT_ROOT', '/home/username/public_html/');

    Then include as follows:

    include DOCUMENT_ROOT . 'includes/bla.php';
     
    premiumscripts, Aug 30, 2009 IP
  11. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #11

    Better,
    
    define('DOCUMENT_ROOT', str_replace('\\', '/', dirname(dirname(__FILE__))));
    include DOCUMENT_ROOT . '/includes/something.php';
    PHP:
     
    Dollar, Aug 30, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Yeah, that's good if you're distributing a script and don't know the exact location, but for my own sites I always try to keep it static. No reason to call dirname if I already know the location :)
     
    premiumscripts, Aug 30, 2009 IP
  13. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #13
    Why not just streamline the needless stuff?
     
    Last edited: Aug 30, 2009
    Dollar, Aug 30, 2009 IP
  14. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ehh, it's just a little optimization. I do what you do during an installation script and then write it to a config file. Less function calls and all that..
     
    premiumscripts, Aug 31, 2009 IP