By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

Backtracking through a directory.

P: n/a
Win
I was wondering if there was a way to backtrack through a directory.
Let's say that we "readdir'd" our way through the directory to the end
and now would like to back up 3 files to read the third from the last.

Is there an easy way to do this or would I have to find the total
number of directory entries and then calculate exactly how many reads
it takes before getting to the desired location?

Any input would be appreciated.

<?php
$dir = '../zone16/misc';
if ( is_dir($dir) && ($dh = opendir($dir)) )
{
while ( ($file = readdir($dh)) !== false )
{
print $file.'<br>';
}
if ( ($file = readdir($dh)) == false )
{
[!!!back up 3 files and read the third to last file!!!]
}
}
?>

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Win wrote:
I was wondering if there was a way to backtrack through a directory.
Let's say that we "readdir'd" our way through the directory to the end
and now would like to back up 3 files to read the third from the last.

Is there an easy way to do this or would I have to find the total
number of directory entries and then calculate exactly how many reads
it takes before getting to the desired location?


Either that, or store the result of the successive readdir()s in an
array.

Jul 17 '05 #2

P: n/a
Win
Thanks for the input, Oli. :-)

Jul 17 '05 #3

P: n/a
The easiest way to work with file listing is glob().

http://www.php.net/glob/

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.