You are here: Re: Help with loop « PHP Programming Language « IT news, forums, messages
Re: Help with loop

Posted by Janwillem Borleffs on 09/30/06 18:04

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация