|
Posted by Scoop on 09/30/06 16:56
Thanks! I tried it out and it works if the file I am looking for is
either in the same directory ($file returns xxxx.yyy) or one level up,
relative to the php code ($file returns ../xxxx.yyy).
I would like it to keep going if the file is, say 2, 3, 4...directories
up and return ../../xxx.yyy, etc. In other words, keep appending ../
until the file is found.
How can I make it do this?
Thanks again for you help.
Janwillem Borleffs wrote:
> Scoop wrote:
> > Can someone tell me what I'm doing wrong?
> >
>
> You're using the wrong function to check if the file exists and you are not
> preventing an infinite loop. Try running the following code:
>
> <?php
> $file = 'somefile';
> $currentdir = '';
> $found = false;
> while (true) {
> // Check if file exists
> if ($found = file_exists($file)) {
> break;
> }
>
> // Prevent infinite loop
> if ($currentdir == realpath(dirname($file))) {
> break;
> }
>
> // Update paths
> $file = "../$file";
> $currentdir = realpath(dirname($file));
> }
>
> // Show result
> echo basename($file),
> $found ?
> " found under: " . realpath(dirname($file)) :
> " not found";
> ?>
>
>
> HTH;
> JW
Navigation:
[Reply to this message]
|