On Thu, 5 Dec 2002, Gundamn wrote:

> I have a hosted account. As such, I am unable to use the default location
> for files when used with the include command. So could somebody tell me how
> I can either make it go to a different directory, or to link to something
> (and how to add the variable as the filename)?
> Thank you in advance.

  My usual approach is to have an includes/ directory, at the same level
  as the htdocs/ directory, and thus outside the webspace.  In this
  includes/ directory I put an include.php file which includes any other
  files needed with include_once, like so:

    include_once INC_DIR . '/config.php';
    include_once INC_DIR . '/functions.php';

  In htdocs/ I put another include.php, which basically says:
    define ('INC_DIR', '../includes');
    include INC_DIR . '/include.php';

  In any subdirectories of htdocs/ I put a similar file, except one level
  deeper it'd be:
    define ('INC_DIR', '../../includes');
    include INC_DIR . '/include.php';

  For each subdir level, add a ../ to the define.

  On a hosted account, you may instead have to put your includes/
  directory elsewhere.  In this case, the include.php in htdocs/ would
  just be
    define ('INC_DIR', '/path/to/includes');
    include INC_DIR . '/include.php';

  and the sub-directory scripts just
    include '../include.php';

  Either way, the goal though is to have an include.php file in each
  directory of your webspace that references a single include.php file
  with relative (../) paths.  Coupled with keeping your includes outside
  the webroot, this can make include files a lot less troublesome.

  A bit of overhead, true, but it keeps my configs, db passwords, and
  library code entirely out of the webspace.  I've used it successfully in
  a large site (150 user-accessible scripts, 25 library scripts, totalling
  about 1M of php)

  One big warning about include files... PHP's include functions work
  counter-intuitively in that all relative paths are relative to the
  script that caught the user's request.  Thus if you have a bunch of
  scripts in a lib/ directory, they can't include each other without
  taking into account the path from the calling script...  Thus why I try
  to define INC_DIR as early as possible if it's relative.

  Hope this helps...

   Morgan Hughes
   C programmer and highly caffeinated mammal.
   ICQ: 79293356

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to