是的,这真的让我很沮丧。
我有3个方向(目录1/目录2/目录3),当一个index.php文件在根目录,我已经张贴在下面。
每个文件夹中都有一个图像列表,这些图像将自动命名并放入每个文件夹中。这些文件在表中显示为3列,在这些列之前还有2列,以显示每个文件的行数和日期和时间。假设3个目录的第一列按DESC排序,以首先显示最新的文件。然而,它以错误的顺序显示日期和时间,例如,在顶部显示2月28日(时间),然后是2月28日(时间),然后是2月26日(时间),然后是更多的28号!?它应该先对最新日期进行排序,然后才是最旧日期。有谁能帮忙吗?
<?php
date_default_timezone_set('Europe/London');
$dirname = "dir1";
$dirnameTwo = "dir2";
$dirnameThree = "dir3";
$cam1 = scandir($dirname, SORT_DESC);
$cam2 = scandir($dirnameTwo, SORT_DESC);
$cam3 = scandir($dirnameThree, SORT_DESC);
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html>
<head>
<meta http-equiv='refresh' content='10'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta http-equiv='cache-control' content='max-age=0' />
<meta http-equiv='cache-control' content='no-cache' />
<meta http-equiv='expires' content='0' />
<meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' />
<meta http-equiv='pragma' content='no-cache' />
</head>
<body>
<style type="text/css">
.pi-title {
padding: 1rem;
}
</style>
<div class="container">
<div class="row">
<div class="pi-title">
<h3>Test</h3>
</div>
<div class="table-container col-md-12">
<table class="table" border='1' cellpadding='5' cellspacing='0' bordercolor='#ccc'>
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">File Name</th>
<th scope="col">1 </th>
<th scope="col">2</th>
<th scope="col">3</th>
</tr>
</thead>
<tbody>
<tr></tr>
<tr>
<?php
error_reporting(E_ALL & ~E_NOTICE);
$directories = implode(',', [ $dirname, $dirnameTwo, $dirnameThree]);
$items = glob("./{{$directories}}/*", GLOB_BRACE);
$dirs = array($dirname, $dirnameTwo, $dirnameThree);
for ($i = 1; $i <= 12; $i++) {
if (file_exists($items[$i])) {
echo "</tr>";
echo "<td><font face='Arial' size='3'>$i</font></td>";
echo "<td><font face='Arial' size='3' color='red'>" . date("F d Y H:i", filemtime($items[$i]));
echo "</font></td>";
}
print("
<td><img src='$dirs[0]/$cam1[$i]' height='80' width='80'></td>
<td><img src='$dirs[1]/$cam2[$i]' height='80' width='80'></td>
<td><img src='$dirs[2]/$cam3[$i]' height='80' width='80'></td>
");
if ($i === 12) break;
}
?>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
发布于 2019-03-01 04:26:06
scandir
按字母顺序升序或降序排序。但您希望按上次修改的日期进行排序。所以你需要对它应用一个排序函数。例如,您可以使用filemtime
和uasort
来执行此操作
$items = glob ...
uasort($items, function($a,$b) { return filemtime($a) > filemtime($b); })
对于asc/desc http://php.net/manual/en/function.scandir.php#refsect1-function.scandir-parameters,将filemtime($a) > filemtime($b)
翻转到filemtime($a) < filemtime($b)
发布于 2019-03-05 06:59:50
这就是我一直在使用的(我已经使用了一段时间的代码片段):
public function scanLatestFile($directory) {
// ignore any files that you don't want scanned
$ignoredFiles = array('.index.php', '.htaccess');
// Create new array to store the list of files
$filesToSearch = array();
// Scan the directory passed in
foreach (scandir($directory) as $file) {
if (in_array($file, $ignoredFiles)) continue;
// Add the filemtime so you can sort it
$filesToSearch[$file] = filemtime($dir . '/' . $file);
}
// Sort by latest file
arsort($filesToSearch);
$filesToSearch = array_keys($filesToSearch);
// Return latest file if it exists
return ($filesToSearch) ? $filesToSearch : false;
}
这将返回目录中的最新文件,忽略文件也可以排除任何你不想要的文件。
https://stackoverflow.com/questions/54933198
复制相似问题