3

I'm using this script to see all subfolders and files of subfolders

   function readfolder($dir)
    {
        global $tfile,$tdir;$i=0;$j=0;$myfiles;
        $myfiles[][] = array();
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file=readdir($dh)) !== false) {
                    if (!is_dir($dir."\\".$file))
                    {
                        $tfile[$i]=$file;
                        $i++;
                        echo $dir."\\".$file." <b>File</b><br>";
                    }
                    else {
                        if (($file != ".") && ($file != "..")) {
                        $tdir[$j]=$file;
                        echo $dir."\\".$file." <b>Directory</b><br>";
                        readfolder($dir."\\".$file);
                        $j++;
                        }
                    }
                }
                closedir($dh);
            }
        }
    }

    readfolder(".");

Can someone tell me how can I use filemtime function (or whatever) so that I can sort subflders and files by modification date?

Goldie
  • 1,570
  • 5
  • 21
  • 33

1 Answers1

8

Have a look at the SPL DirectoryIterator. It's cleaner than what you're currently doing and trivial to make it recursive. It also has a suitable mtime method.

Nate
  • 12,499
  • 5
  • 45
  • 60
Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
  • 1
    There's even a recursive directory iterator in the spl, see http://docs.php.net/class.recursivedirectoryiterator – VolkerK Aug 31 '11 at 09:11
  • Thanks @VolkerK — not sure how I missed / didn't remember that. In my defence, it's still early! – Nev Stokes Aug 31 '11 at 09:14