Problem using includes in subdirectories

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seegoon
    New Member
    • Oct 2009
    • 31

    Problem using includes in subdirectories

    This must be a really basic problem for almost every site out there.

    How can I get includes to work for subdirectories (i.e. mysite.com/subdirectory/file.php) when all of my includes are stored in a /includes folder off the root directory? I've been Googling and have come across dozens of cryptic solutions.

    It would be great if it was a .htaccess thing, because I can foresee that being the simplest way. That way, you can still use simple php to include the file, right?

    I had an answer to this posted by Dormilich before, but it was too complex for me to get my noobish head around. Any step-by-step advice would be welcomed with open arms!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    A simple way is to determine the physical location of the include director and create a constant in the index file.

    For example, if your "includes" directory is located at "/var/www/inludes", then you could do:
    [code=php]<?php
    define("INCLUDE _DIR", "/var/www/includes/");

    include INCLUDE_DIR . "dbStuff.ph p";
    include INCLUDE_DIR . "otherStuff.php ";
    ?>[/code]
    And then in any file that is included after that point, you can use the INCLUDE_DIR constant to include files.

    There are ways to find the location of the file automatically as well. For instance, if you know the "includes/" dir is always in the same directory as the index file, you can replace the previous constant with this:
    [code=php]define('INCLUDE _DIR', dirname($_SERVE R['SCRIPT_FILENAM E']) . "/includes/");[/code]

    Comment

    • seegoon
      New Member
      • Oct 2009
      • 31

      #3
      Sorry, this really is a newb question - but what do you mean by 'index file'? Do you mean www.mysite.com/index.php?

      Comment

      • seegoon
        New Member
        • Oct 2009
        • 31

        #4
        Oh, and this might complicate things a bit: I'm using a testing site to develop this site. Its domain is test.mysite.com , but all the files actually reside in mysite.com/test. Can I just perform any changes in the /test/ subdir, and once I want to go live, transfer to the main site?

        Sorry to make things more difficult!

        Comment

        • seegoon
          New Member
          • Oct 2009
          • 31

          #5
          I found a solution; I don't know if it's the most elegant out there, but it sure seems to work.

          Altering the php.ini in my root directory (in my case, php5.ini) and adding the following line:
          Code:
          include_path=".:/includes"
          I guess it won't work for everyone, but it means that I only have to write the following line, on any page, to call an include:

          Code:
          <?php include("include.php"); ?>
          Simples.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Yep, that is also a very good way.

            If you don't have access the the configuration file itself, you can do that same by using the set_include_pat h or ini_set functions.
            Originally posted by http://php.net/set_include_pat h
            [code=php]<?php
            // Works as of PHP 4.3.0
            set_include_pat h('/inc');

            // Works in all PHP versions
            ini_set('includ e_path', '/inc');
            ?>[/code]
            You need to be careful, though, because the include path can contain more than one path, and if you overwrite it like the previous example did the old paths will be lost, which may break other parts of the code. You can avoid that by doing:
            Originally posted by http://php.net/set_include_pat h
            [code=php]<?php
            $path = '/usr/lib/pear';
            set_include_pat h(get_include_p ath() . PATH_SEPARATOR . $path);
            ?>[/code]

            Comment

            Working...