Jump to content

sort files in directory by date


mallen

Recommended Posts

I have this code below that lists all files uploaded by users. The problem is they are in no order. I want to sort them by date. How can i do this?

 

$x = 0;
function parse_dir($dir) {
   if($dh = @opendir($dir)) {
       while(($file = readdir($dh)) !== false) {
   	if($x == 0)
		{	
			$color = "#CCCCCC";
		} else {
			$color = "#EDECEC";
		}
           if( !preg_match('/^\./s', $file) )  {
               if(is_dir($dir.$file)) {
                   //nothing
               } else {
                   echo '<tr>
    <td bgcolor='.$color.' class="style2"><a href="https://xxxxxx/'.$dir.$file.'">'.$dir.$file. ' 
                 '.   date(  'm,j,y',filemtime("$dir$file")).'</a></td>
    <td width="20" bgcolor='.$color.' class="style2"> </td>
    <td width="80" bgcolor='.$color.' class="style2"><a href="delete.php?file='.$file.'">Delete</a></td>
  </tr>';
  $x = $x + 1;
  if ($x == 2)
  	{
	$x = 0;
}
               }
           }            
       }
       chdir('..');
   }
}
parse_dir('upload/');
?>


Link to comment
https://forums.phpfreaks.com/topic/185483-sort-files-in-directory-by-date/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.