473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with loop

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?

Sep 30 '06 #1
4 1288
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
Sep 30 '06 #2
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
Sep 30 '06 #3
Scoop wrote:
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
Sep 30 '06 #4
Thanks for your help. I've got everything working the way I wanted it
to.

Sep 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William Buchanan | last post by:
Hi I have the following stored proc in interbase (which might contain errors - i'm doing it off the top of my head), which I would like to convert into oracle. Can you help? What I want back is...
1
by: nightsaber | last post by:
<script language="JavaScript"> <!-- hide me var the_number = prompt("how many words (3-5 is good)?", "4"); var the_string = ""; var a_word; for (loop = 0; loop < the_number; loop++) {...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.