Re: Opendir on site root directory?
| From: | Nathan Rixham | Date: | Mon, 09 Mar 2009 14:01:32 +0000 |
| Subject: | Re: Opendir on site root directory? | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-289644@lists.php.net to get a copy of this message | ||
Stuart wrote:
2009/3/8 Clancy <clancy_1@cybec.com.au>lol stut! - just-in-case I'll add in that "__FILE__" is a magic php constant holding the path to the file it is used in, and not just stuarts way of saying "put your filename here" regards!On Sun, 8 Mar 2009 12:33:15 +0000, stuttle@gmail.com (Stuart) wrote:This should be irrelevant in your code, something you can achieve using dirname(__FILE__). My webpage is always launched by loading index.php from the root directory,2009/3/8 Clancy <clancy_1@cybec.com.au>runOn Sun, 8 Mar 2009 09:01:18 +0000, stuttle@gmail.com (Stuart) wrote:2009/3/8 Clancy <clancy_1@cybec.com.au>I want to index the files on a website recursively. The program willrootinanythe site root directory, which GETCWD reports as D:/Websites/Website_1. I can openopenfile in the root directory simply using its file name; Joe.dat, for example, and I can opendir for any subdirectory; egopendir(Subdirectory_1);but opendir () does not seem to work, and the only way I can find tothe root directory is to give its full path; egopendir (D:/Websites/Website_1);I have got the program working by using the full path to open therootbutdirectory, and then using relative paths to open the subdirectories and individual files,this seems rather a kludge, and I am wondering if there is a way to open theneededdirectory 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 asbothtoget from the current file to the document root you're after.It has always worked for me. But then I never change directory. Butgivesgive a hardware dependent answer; echo '<p>Current directory is '.__FILE__.', CWD is '.getcwd().'</p>';isCurrent directory is D:\Websites\Corybas\Cydaldev\Dev\Testbed_2.php, CWDI 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'.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.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.Either I'm not explaining this well enough or you're just not getting it. Ignore the current working directory - it's not reliable. If every page is created by a single script then the solution is to define a constant at the top of that script that gives you the directory that script is in.... define('ROOT_DIR', dirname(__FILE__)); Then when you need to refer to another file in your directory structure you do so relative to the location of index.php. So if you have a directory named arse in the same folder as index.php you'd refer to that like so... ROOT_DIR.'/arse' If that's not clear maybe this will help: http://dev.stut.net/php/dirname.phpBased on that the following should work in your particular situation, butrather than just using this I encourage you to understand why it works...You clearly haven't looked into this at all since you're still not using dirname(__FILE__), and if this email complete with the example script above doesn't get the point across I give up. Have you even looked up dirname in the manual? http://php.net/dirname As a result of what seems to me to be an oversight in the design ofopendir(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.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.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.If, as it seems, I have to accept this it is fairly immaterial which kludge I actually use.There is no need to use a "kludge". I encourage you to read my replies carefully if you're still not getting it. -Stuart