[英]php scandir sorting by file date
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">';
}
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) (我只是用glob()
获取目录的所有文件,然后我用usort()
对数组进行排序,在那里我使用filemtime()
来比较最后一次修改,并使用 foreach 循环遍历每个文件)
<?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">';
}
?>
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);
?>
Another scandir keep latest 5 files另一个 scandir 保留最新的 5 个文件
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]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.