Re: Opendir on site root directory?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/3/10 Clancy <clancy_1@xxxxxxxxxxxx>

> On Mon, 9 Mar 2009 10:07:33 +0000, stuttle@xxxxxxxxx (Stuart) wrote:
>
> .......
> >As in the example script I've posted above you can refer to the current
> >working directory with a single period (.), but this is still relying on
> the
> >current working directory being what you expect it to be.
>
> Thank you! This is what I had been looking for all along. I dimly
> remembered some such
> trick, but it had become lost in the detritus at the back of my mind.
>
> But why the obsession with avoiding getcwd()? When my site is loaded the
> current directory
> is always the root directory of the page, and as I never change directory I
> can rely on it
> staying there. (And, incidentally, since getcwd(), dirname(__FILE__), etc,
> all return the
> complete path including system dependent information, if I really didn't
> know the current
> directory it would not be a trivial task to determine what was the local
> root.)


Relying on the current working directory may have worked for you so far, but
that doesn't mean it always will. When I write code I make sure it's as
portable as possible because I've been burned before by issues such as this.

The current working directory is set one of a number of components in your
stack depending upon which HTTP server and SAPI you're using. To assume that
it will always be the directory any given script is in is simply asking for
trouble.

By using dirname(__FILE__) to get a known location within your source tree
you can build absolute paths using relative terms meaning it is independent
of any configuration or local state.

For a couple of years I have been using getcwd() to determine whether or not
> I am running
> on my local PC (if I am the path will start with 'D:'). If I am running on
> the local PC I
> load various editing and other private facilities, otherwise I omit them.
> This is done in
> such a way that there is nothing to indicate that anything is either
> missing or has been
> omitted.


I think the only suitable thing I can say to that is.... ewwwww!!!

-Stuart

-- 
http://stut.net/

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux