note 24652 deleted from function.readdir by tularis

From: Date: Sun, 13 May 2007 19:07:31 +0000
Subject: note 24652 deleted from function.readdir by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126561@lists.php.net to get a copy of this message
Note Submitter: eric.draven[AT]tiscalinet[DOT]it ---- 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

« previous php.notes (#126561) next »