Traverse directories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RoundKill

    Traverse directories

    My problem is that I can't find a way of referencing files and
    includes statically. I have to change each page reference so that when
    it is referenced it references the correct document. As I want a
    central location to keep all my most used 'includes'.

    So if I change a scripts location I also have to change the script
    it's self to point to the correct documents. This is a problem when
    the directory structures hierarchy keeps changing.

    Also HTTP referencing is out as the scripts will not all be run on the
    same servers.

    Any help would be appreciated as simple problem as it seems to be, I
    can;t find an answer.

    Thanks
  • Zac Hester

    #2
    Re: Traverse directories

    "RoundKill" <sways@yahoo.co m> wrote in message
    news:52dbc2a.03 07291312.690026 2a@posting.goog le.com...[color=blue]
    > My problem is that I can't find a way of referencing files and
    > includes statically. I have to change each page reference so that when
    > it is referenced it references the correct document. As I want a
    > central location to keep all my most used 'includes'.
    >
    > So if I change a scripts location I also have to change the script
    > it's self to point to the correct documents. This is a problem when
    > the directory structures hierarchy keeps changing.
    >
    > Also HTTP referencing is out as the scripts will not all be run on the
    > same servers.
    >
    > Any help would be appreciated as simple problem as it seems to be, I
    > can;t find an answer.
    >
    > Thanks[/color]

    If you have a special directory that all of your "include" files are kept,
    put the full, filesystem path to those included documents in the arguement
    to the include() function:

    include('/usr/local/apache/htdocs/php-inc/myresource.php' );

    Then, no matter where your script is called from, this file will always be
    accessible and you don't have to change a thing.

    If you're deploying this on different web servers where the absolute path
    might change, I make a "CONFIG" array and store any useful information in
    it:

    $CONFIG['webroot'] = '/usr/local/apache/htdocs/';

    Then, you can use this everywhere in the global scope of your scripts:

    include($CONFIG['webroot'].'myresource.ph p');

    Hardcoding absolute paths can be a pain otherwise.

    HTH,
    Zac


    Comment

    • Keith Maika

      #3
      Re: Traverse directories

      RoundKill wrote:[color=blue]
      > My problem is that I can't find a way of referencing files and
      > includes statically. I have to change each page reference so that when
      > it is referenced it references the correct document. As I want a
      > central location to keep all my most used 'includes'.
      >
      > So if I change a scripts location I also have to change the script
      > it's self to point to the correct documents. This is a problem when
      > the directory structures hierarchy keeps changing.
      >
      > Also HTTP referencing is out as the scripts will not all be run on the
      > same servers.
      >
      > Any help would be appreciated as simple problem as it seems to be, I
      > can;t find an answer.
      >
      > Thanks[/color]

      You can use php's __FILE__ constant. This is relative to the current
      script being excuted and does change with includes. So
      dirname(__FILE_ _) would always yield the directory that the current file
      is in. This means you can do includes like this in a common file:

      include(dirname (__FILE__).'/whatever.php');
      include(dirname (__FILE__).'/../whatever2.php') ;
      include(dirname (__FILE__).'/classes/whatever.php');

      and then the will work no matter where the other file you include that
      common file into is located. I use this quite often on my projects.

      (PS. Sorry if this appears multiple times, it doesn't want to say that
      it's sending it)

      --
      http://kicken.mine.nu:8008/ - Personal Site
      http://wiser.kicks-ass.org:8008/ - Long-term Project
      Keith Maika

      Comment

      Working...