phpfilemtime

How to find out oldest file from a directory??? (PHP)


I have a directory named 'backup'

its contains following files (with some specific details)

No---File Name----------------------------------------------------------------Date--------------------Time-----------Size

1.  blog-backup_on_11-April-2012_at_08-22-18AM.zip  11-April-2012   08:22:18AM  5.28MB
2.  blog-backup_on_11-April-2012_at_08-25-24AM.zip  11-April-2012   08:25:25AM  5.28MB
3.  blog-backup_on_11-April-2012_at_08-25-46AM.zip  11-April-2012   08:25:47AM  5.28MB
4.  blog-backup_on_11-April-2012_at_08-26-07AM.zip  11-April-2012   08:26:08AM  5.28MB
5.  blog-backup_on_11-April-2012_at_08-27-52AM.zip  11-April-2012   08:27:53AM  5.28MB

I want to get oldest file from 'backup' directory
Like 'blog-backup_on_11-April-2012_at_08-22-18AM.zip' its the oldest file in directory.

I tried this link but unable to handle logic.

Please give some suggestion, how to get this file?


Solution

  • $dir = 'path/to/backup/';
    
    foreach (glob($dir.'*.zip') as $filename) {
      $time = filemtime($filename);
      $files[$time] = $filename;
    }
    krsort($files);
    
    echo reset($files);