You can use PHP's built-in functions. glob() Retrieve files with a specified extension from a designated directory, then use them. filectime() The function retrieves the creation time of each file and stores the file names along with their creation times in an array; then it is used. usort() The function sorts the array based on its creation time; then, it simply iterates through the array and prints the file names.
Here is an example code snippet:
<?php
// 指定目录和后缀
$dir = '/path/to/directory/';
$extension = 'txt';
// 获取指定后缀的文件
$files = glob($dir . '*.' . $extension);
// 保存文件名和创建时间的数组
$file_list = array();
// 遍历文件
foreach ($files as $file) {
// 获取文件的创建时间
$ctime = filectime($file);
// 将文件名和创建时间保存到数组中
$file_list[] = array(
'name' => $file,
'ctime' => $ctime
);
}
// 按照创建时间排序
usort($file_list, function($a, $b) {
return $a['ctime'] - $b['ctime'];
});
// 输出文件名
foreach ($file_list as $file) {
echo $file['name'] . PHP_EOL;
}
?>In the above example code, the relevant parts need to be replaced. $dir 和 $extension The variables represent the directory path and file extension for the file to be read. Use. glob() The function retrieves files with a specified suffix, then iterates through these files to extract their creation times, and stores both the file names and their creation times into an array. Finally, it is used. usort() The function sorts an array based on the creation time and then iterates through the array to output the file names.