Reputation: 55
I need help, how to sort the code below, by file date?.
$dir2 = "flash/$plk/img";
$plks2 = scandir($dir2);
$plkss2 = array_diff($plks2, array('.', '..'));
foreach ($plkss2 as $plk2) {
echo '<img data-src="flash/'. str_replace('+', '%20', urlencode($plk)) .'/img/' . $plk2 . '" alt="" class="img-responsive lazyload">';
}
Upvotes: 2
Views: 8488
Reputation: 9
Another scandir keep latest 5 files
public function checkmaxfiles()
{
$dir = APPLICATION_PATH . '\\modules\\yourmodulename\\public\\backup\\'; // '../notes/';
$ignored = array('.', '..', '.svn', '.htaccess');
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, $ignored)) continue;
$files[$file] = filemtime($dir . '/' . $file);
}
arsort($files);
$files = array_keys($files);
$length = count($files);
if($length < 4 ){
return;
}
for ($i = $length; $i > 4; $i--) {
echo "Erase : " .$dir.$files[$i];
unlink($dir.$files[$i]);
}
}
Upvotes: 0
Reputation: 22
here you go
<?php
$dir = ".";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
$file_array = array();
foreach ($files as $file_name) {
$file_array[filemtime($file_name)] = $file_name;
}
ksort($file_array);
var_dump($file_array);
?>
Upvotes: 0
Reputation: 59681
This should work for you:
(I just get all files of the directory with glob()
, then I sort the array with usort()
, where I use filemtime()
to compare the last modification and the I loop through every file with the foreach loop)
<?php
$files = glob("flash/$plk/img/*.*");
usort($files, function($a, $b){
return filemtime($a) < filemtime($b);
});
foreach ($files as $plk2) {
echo '<img data-src="flash/' . str_replace('+', '%20', urlencode($plk)) . '/img/' . $plk2 . '" alt="" class="img-responsive lazyload">';
}
?>
Upvotes: 7