Re: Opendir on site root directory?

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

 



On Sun, 8 Mar 2009 12:33:15 +0000, stuttle@xxxxxxxxx (Stuart) wrote:

>2009/3/8 Clancy <clancy_1@xxxxxxxxxxxx>
>
>> On Sun, 8 Mar 2009 09:01:18 +0000, stuttle@xxxxxxxxx (Stuart) wrote:
>>
>> >2009/3/8 Clancy <clancy_1@xxxxxxxxxxxx>
>> >
>> >> I want to index the files on a website recursively. The program will run
>> in
>> >> the site root
>> >> directory, which GETCWD reports as D:/Websites/Website_1.  I can open
>> any
>> >> file in the root
>> >> directory simply using its file name; Joe.dat, for example, and I can
>> >> opendir for any
>> >> subdirectory; eg
>> >>
>> >>        opendir(Subdirectory_1);
>> >>
>> >> but opendir () does not seem to work, and the only way I can find to
>> open
>> >> the root
>> >> directory is to give its full path; eg
>> >>
>> >>        opendir (D:/Websites/Website_1);
>> >>
>> >> I have got the program working by using the full path to open the root
>> >> directory, and then
>> >> using relative paths to open the subdirectories and individual files,
>> but
>> >> this seems
>> >> rather a kludge, and I am wondering if there is a way to open the root
>> >> directory without
>> >> specifying an absolute path?
>> >
>> >
>> >The current working directory cannot be trusted to be "right". The best
>> >option is to use dirname(__FILE__) and add '/..' as many times as needed
>> to
>> >get from the current file to the document root you're after.
>>
>> It has always worked for me.  But then I never change directory. But both
>> give a hardware
>> dependent answer;
>>
>> echo '<p>Current directory is '.__FILE__.', CWD is '.getcwd().'</p>'; gives
>>
>> Current directory is D:\Websites\Corybas\Cydaldev\Dev\Testbed_2.php, CWD is
>> D:\Websites\Corybas.
>
>
>Not sure what you mean by a hardware-dependent answer. The current working
>directory for any given script is determined by the web server and so it
>cannot be assumed to be the location of the current script.
>dirname(__FILE__) will give you the directory the current script is in.

I agree that 'hardware dependent' was not the right word, but what I meant was that if I
am running the local version of my program getcwd() will return 'D:\Websites\Corybas',
where as if I am running the remote version it will return 'home/Corybasftp/www'.

My webpage is always launched by loading index.php from the root directory, and I don't
think I ever change the working directory, so although the code actually being executed at
any given time is usually in a subdirectory, getcwd() will reliably return the (system
dependent) long definition of the root directory.
>

>Based on that the following should work in your particular situation, but
>rather than just using this I encourage you to understand why it works...
>opendir(dirname(__FILE__).'/../Cydaldev');

Looking into this I did find that 'opendir('Albums/../');', where 'Albums' can be any
subdirectory I know to be present, will reliably open the root directory.

Initially I built my index starting from the path returned by getcwd(), but when I got it
working I realised that, as it contained the system dependent information, it would be
useless on the remote server.

As a result of what seems to me to be an oversight in the design of opendir(), in that
opendir(Fred) and opendir(Fred/Nurg) will open the appropriate sub directories but
opendir() will not open the root directory, I can readily index all the subdirectories by
giving their relative paths, but I have to resort to a kludge to index the root directory,
and another kludge to eliminate this kludge from the index. 

If, as it seems, I have to accept this it is fairly immaterial which kludge I actually
use.


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


[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