Jump to content

Sort a directory by filename only showing the latest file of each type based on the name


Recommended Posts

I have these files in a directory and can easily list to a webpage 

 

Daily_CD_20130902.xml
Daily_CD_20130903.xml
Daily_CO_20130902.xml
Daily_CO_20130903.xml
Daily_CU_20130902.xml
Daily_CU_20130903.xml
Daily_EQ_20130902.xml
Daily_EQ_20130903.xml
Daily_IR_20130902.xml
Daily_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.xml
Daily_CO_20130903.xml
Daily_CU_20130903.xml
Daily_EQ_20130903.xml
Daily_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 :)

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;
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.