ginkworm Posted September 3, 2013 Share Posted September 3, 2013 I have these files in a directory and can easily list to a webpage Daily_CD_20130902.xmlDaily_CD_20130903.xmlDaily_CO_20130902.xmlDaily_CO_20130903.xmlDaily_CU_20130902.xmlDaily_CU_20130903.xmlDaily_EQ_20130902.xmlDaily_EQ_20130903.xmlDaily_IR_20130902.xmlDaily_IR_20130903.xml However, i want to be selective and just select the most recent of each type, so instead of the whole list i want to display Daily_CD_20130903.xmlDaily_CO_20130903.xmlDaily_CU_20130903.xmlDaily_EQ_20130903.xmlDaily_IR_20130903.xml Is there an easy way to do this or do i need to take apart the filename, put the date in a date object and then use it to sort. my current code is: public function getFiles() { //return String, form as HTML later $return = ""; // create a resrouce pointing to the directory with the files in it on disk $dir = opendir('DIRECTORY'); //path to that directory that is appended to the URL $actualdir = 'PATH FROM WEBSERVER ROOT'; // read the directory looping over the files and populate to an array while ($entryName = readdir($dir)){ $dirArray[] = $entryName; } //close the directory as we don't need it anymore closedir($dir); //make a note of how many items are in the directory $indexCount = count($dirArray); //loop through the files building up a list of links for($index=0; $index < $indexCount; $index++) { if($dirArray[$index] !="." && $dirArray[$index] !=".."){ $return .= "<a href='"."$actualdir".$dirArray[$index]."'>".$dirArray[$index]."</a><br/>"; } } return $return; } any help is appreciated as this is my first day of PHP Quote Link to comment https://forums.phpfreaks.com/topic/281826-sort-a-directory-by-filename-only-showing-the-latest-file-of-each-type-based-on-the-name/ Share on other sites More sharing options...
Psycho Posted September 3, 2013 Share Posted September 3, 2013 Try this: public function getFiles() { // create a resrouce pointing to the directory with the files in it on disk $dir = opendir('DIRECTORY'); //path to that directory that is appended to the URL $actualdir = 'PATH FROM WEBSERVER ROOT'; // read the directory looping over the files and populate into // two arrays: one to tracjk the data and one to track the name $typesByDate = array(); $typesByName = array(); while ($fileName = readdir($dir)) { //Skip directories if(!is_file($fileName)) { continue; } //Get type and date from file name $type = substr($fileName, 6, 2); $date = substr($fileName, 6, ; //If no file set for this type or date is newer than last saved, add it if(!isset($typesByDate[$type]) || $typesByDate[$type] < $date) { $typesByDate[$type] = $date; $typesByName[$type] = $fileName; } } //close the directory as we don't need it anymore closedir($dir); //loop through the files building up a list of links $return = ''; foreach($typesByName as $file) { $return .= "<a href='{$actualdir}{$file}'>{$dirArray[$file}</a><br/>"; } return $return; } Quote Link to comment https://forums.phpfreaks.com/topic/281826-sort-a-directory-by-filename-only-showing-the-latest-file-of-each-type-based-on-the-name/#findComment-1448030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.