Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with loop

Scoop
Guest
 
Posts: n/a
#1: Sep 30 '06
I am new to php and not much of a developer to begin with. I am trying
to write what seems like it should be a simple piece of code but I
can't get it to work.

I'm trying to check if a file exists. If it is in the directory where
the php page is, I'd like to echo the name of the file. If it doesn't
exist, I'd like to back up a directory and check again. I'd like it to
continue backing up, one directory at a time until the file is found,
then echo the relative file location as ../../file.html where there is
one ../ per directory that needed to be traversed. Here is what I
have:

<?php
$file_path = 'file.htm';
do {
$globcheck = glob($file_path);
$homefile_path = '../'.$file_path;
} while(!empty($globcheck));
?>
<?php echo $file_path; ?>

Can someone tell me what I'm doing wrong?


Janwillem Borleffs
Guest
 
Posts: n/a
#2: Sep 30 '06

re: Help with loop


Scoop wrote:
Quote:
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


Scoop
Guest
 
Posts: n/a
#3: Sep 30 '06

re: Help with loop


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:
Quote:
Scoop wrote:
Quote:
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
Janwillem Borleffs
Guest
 
Posts: n/a
#4: Sep 30 '06

re: Help with loop


Scoop wrote:
Quote:
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?
>
Sorry, the script contained an error; try the following:

<?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 path
$currentdir = realpath(dirname($file));
$file = "../$file";
}

// Show result
echo basename($file),
$found ?
" found under: " . realpath(dirname($file)) . "; path: $file" :
" not found";

?>


JW


Scoop
Guest
 
Posts: n/a
#5: Sep 30 '06

re: Help with loop


Thanks for your help. I've got everything working the way I wanted it
to.

Closed Thread