Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last X files created from a directory (PHP)

Im using this code to get the most recent file created:

<?php 

    $files = glob($siteRoot.'/dir/*/*.php');
    $files = array_combine($files, array_map('filectime', $files));
    arsort($files);
    echo key($files); 

?>

How can I enhance this to get the last 3 files created, or what ever number I need? Any help would be appreciated. Thanks!

like image 662
trobbins26 Avatar asked Oct 17 '25 15:10

trobbins26


1 Answers

This should work for you:

Just take an array_slice() from your array:

$slice = array_slice($files, 0, 3); 
like image 172
Rizier123 Avatar answered Oct 20 '25 03:10

Rizier123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!