Backtracking through a directory.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Win

    Backtracking through a directory.

    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!!!]
    }
    }
    ?>

  • Oli Filth

    #2
    Re: Backtracking through a directory.

    Win wrote:[color=blue]
    > 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?[/color]

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

    Comment

    • Win

      #3
      Re: Backtracking through a directory.

      Thanks for the input, Oli. :-)

      Comment

      • Chung Leong

        #4
        Re: Backtracking through a directory.

        The easiest way to work with file listing is glob().

        Find pathnames matching a pattern


        Comment

        Working...