note 24652 added to function.readdir

From: Date: Sat, 24 Aug 2002 16:39:48 +0000
Subject: note 24652 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-35547@lists.php.net to get a copy of this message
Sometimes happens that you need to sort your files by date , but using associative arrays you won't be able to list files that were written in the same second (rarely , but with CMS system it's strangely frequent , doh!). Here's something that may come in hand in such a case. Code CAN really be improved (especially flags), but you can figure out the trick. $dir = opendir($myDir); while (($file = readdir($dir)) !== false) { if ($file != "." and $file != "..") { $fileTime=filemtime($docsPath.$file); $stillExists = true; while ($stillExists) { $stillExists = validateTs($fileTime); if (!$stillExists) { $articlesToProcess[$fileTime] = $file; } else { echo("<font color=\"red\" size=\"3\">found ts equals to another key. add 1 second and retry </font><br>"); $fileTime++; } } } } krsort($articlesToProcess); # Follow function that do the work function validateTs($fileTime) { global $articlesToProcess; foreach($articlesToProcess as $key => $value) { if ($fileTime == $key) { return true; } } return false; } email me for comments. Marcus -- http://www.php.net/manual/en/function.readdir.php http://master.php.net/manage/user-notes.php?action=edit+24652 http://master.php.net/manage/user-notes.php?action=delete+24652 http://master.php.net/manage/user-notes.php?action=reject+24652

« previous php.notes (#35547) next »