Page 1 of 1

list files by ascending date in start of filename

Posted: Tue Nov 25, 2008 6:59 am
by Sindarin
I have a folder filled with zip files

The format the files are named is DD-MM-YYYY - my file title.zip
Is there any way to sort those files by their DD-MM-YYYY prefix in their filename?

The file list should be like,

25-11-2008 my archive title 1
24-11-2008 my archive title 2
24-11-2008 my archive title 3
22-11-2008 my archive title 4
14-10-2008 my archive title 5
18-12-2007 my archive title 6

I use the following php code to list and paginate the files in the folder 'upload':

Code: Select all

function dirList ($directory, $sortOrder){
 
    //Get each file and add its details to two arrays
    $results = array();
    $handler = opendir($directory);
    while ($file = readdir($handler)) { 
        if ($file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
            $currentModified = filectime($directory."/".$file);
            $file_names[] = $file;
            $file_dates[] = $currentModified;
        }   
    }
       closedir($handler);
 
//clear text file
$fhandle=fopen('archives/entries-t.txt','w') ;
fwrite($fhandle,"");
fclose($fhandle);
 
    //Sort the date array by preferred order
    if ($sortOrder == "newestFirst"){
        arsort($file_dates);
    }else{
        asort($file_dates);
    }
   
    //Match file_names array to file_dates array
    $file_names_Array = array_keys($file_dates);
    foreach ($file_names_Array as $idx => $name) $name=$file_names[$name];
    $file_dates = array_merge($file_dates);
   
    $i = 0;
 
    //Loop through dates array and then echo the list
    foreach ($file_dates as $file_dates){
        $date = $file_dates;
        $j = $file_names_Array[$i];
        $file = $file_names[$j];
        $i++;
 
//write entries used for pagination
 
$fhandle=fopen('archives/entries-t.txt','a') ;
fwrite($fhandle,"<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-7' /></head><body>
<left><a href='/archives/upload/$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "</a></left><br>");
fclose($fhandle);
    }
 
}
 
 
dirlist("E:/Sites/mysite.com/archives/upload","newestFirst");
 
 
 
//pagination start
$pagenum = 1;
$perpage = 15;
$filename="archives/entries-t.txt";
 
    $array = file($filename);
    $count = count($array);
    if(!$_GET['page'])
        {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
 
    for($i=0;$i<=$count;$i++)
    {
        if($array[$i])
        {
            $array2[$i]['info'] = $array[$i];
            if($i2==($perpage-1))
            {
                $array2[$i]['page'] = $pagenum++;
                $i2 = 0;
            }
            else
            {
                $array2[$i]['page'] = $pagenum;
                $i2++;
            }
        }
    }
 
$numpages = $array2[count($array2)-1]['page'];
 
    foreach($array2 as $iarray)
    {
        if($iarray['page'] == $page)
        {
            echo $iarray['info']."<br>\n";
        }
    }echo "<br>??????: ";
 
    for($i3=1;$i3<=$numpages;$i3++)
    {
        if($page == $i3)
 
            echo '['.$i3.']';
        else            
                                      echo '  <a href="'.$PHP_SELF.'?page='.$i3.'">'.$i3.'</a> ';
    }
//pagination end
But they appear by modified date, which some of them changed when there was a server transition and caused problems.

Re: list files by ascending date in start of filename

Posted: Tue Nov 25, 2008 12:16 pm
by Mark Baker

Code: Select all

 
<?php
 
$fileList = array( '25-11-2008 my archive title 1',
                   '24-11-2008 my archive title 2',
                   '24-11-2008 my archive title 3',
                   '22-11-2008 my archive title 4',
                   '14-10-2008 my archive title 5',
                   '18-12-2007 my archive title 6'
                 );
 
function dateSort($file1,$file2) {
    $datedFile1 = substr($file1,6,4).substr($file1,3,2).substr($file1,0,2).substr($file1,11);
    $datedFile2 = substr($file2,6,4).substr($file2,3,2).substr($file2,0,2).substr($file2,11);
    if ($datedFile1 == $datedFile2) {
        return 0;
    }
    return ($datedFile1 < $datedFile2) ? -1 : 1;
}
 
function dateSortReversed($file1,$file2) {
    $datedFile1 = substr($file1,6,4).substr($file1,3,2).substr($file1,0,2).substr($file1,11);
    $datedFile2 = substr($file2,6,4).substr($file2,3,2).substr($file2,0,2).substr($file2,11);
    if ($datedFile1 == $datedFile2) {
        return 0;
    }
    return ($datedFile1 < $datedFile2) ? 1 : -1;
}
 
usort($fileList,'dateSort');
 
echo '<pre>';
print_r($fileList);
echo '<pre>';
 
echo '<hr />';
 
usort($fileList,'dateSortReversed');
 
echo '<pre>';
print_r($fileList);
echo '<pre>';
 
?>