Find the oldest file in directory - Php

Oldest file

Snippet Code


  
Rate this page :
  [ 0 votes]

This function is used to find the oldest file in a directory.

<?php function getOldestFile($directory) { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if (is_file($directory.$file)) { $files[] = $file; } } if (count($files) <= 12) { return; } else { foreach ($files as $val) { if (is_file($directory.$val)) { $file_date[$val] = filemtime($directory.$val); } } } } closedir($handle); asort($file_date, SORT_NUMERIC); reset($file_date); $oldest = key($file_date); return $oldest; } echo getOldestFile("/path/to/your/directory/", 8); ?>

Tags


Ask Questions

Ask Question