Php 在目录中包含日期为的文件
好的,我对我过去/现在工作的网站有一个概述。对于每个站点,我都有一个php文件。在这个php文件中,我使用这段代码来获取一个文件的最新和最旧日期,即目录中的一张图片Php 在目录中包含日期为的文件,php,arrays,date,include,directory,Php,Arrays,Date,Include,Directory,好的,我对我过去/现在工作的网站有一个概述。对于每个站点,我都有一个php文件。在这个php文件中,我使用这段代码来获取一个文件的最新和最旧日期,即目录中的一张图片 $date = "test/*.*"; $files = array_filter(glob($date), function($file) { $ext = substr($file, strrpos($file, '.')); return !in_array($ext, array('.jpg', '.bit'
$date = "test/*.*";
$files = array_filter(glob($date), function($file) {
$ext = substr($file, strrpos($file, '.'));
return !in_array($ext, array('.jpg', '.bit', '.png', '.jpeg', '.gif', '.bmp'));
});
$latest = count($files)-1 ;
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);
$newestfile = date ("d F Y ", filemtime($files[0]));
$oldestfile = date ("d F Y ", filemtime($files[$latest]));
if($newestfile == $oldestfile) {
echo date ("d F Y ", filemtime($files[0]));
} else {
echo date ("d F Y ", filemtime($files[0]));
echo " - " ;
echo date ("d F Y .", filemtime($files[$latest]));
}
该代码的输出如下:2013年1月16日至2013年10月25日
在我的概述页面中,我使用一个代码将所有php文件(我创建的网站)包含到该页面中。(顺便说一句,php文件不是很大的页面,只有一张图片和一点文本。)
此阵列的输出如下所示:
Array (
[0] => sites/test.php
[1] => sites/test2.php
[2] => sites/test3.php
到目前为止还不错,没有问题
现在,我想对包含的文件进行排序,而不是按照包含文件的时间进行排序,就像我在上面的代码中所做的那样。但是我想让它按照目录中文件的最新日期排序,就像php文件一样。所以实际上我想把这些代码合并成一个。
所以我有一个名为test的php文件。我想要目录中最新文件的日期,也称为test。它们总是同名的
我的想法是使用第二个代码的输出,然后去掉“sites/”和“.php”。我想那些名字一定在一个数组中。然后为每个名称获取最新的文件,并从最新到最旧进行排序
我想,就像这样,我在页面的顶部看到了我最近工作的网站,而在页面的底部看到了旧的网站。
也许我的方法是完全错误的,但我不知道如何在代码中做到这一点。看看这一点:
array_map( 'filemtime', $listy ),
在这里,您可以有效地将文件列表转换为修改日期列表。把它转换成另一个函数怎么样。在目录中查找最新文件的人:
array_map(function ($filename) {
// $filename = sites/test1.php
$dir = substr($filename, strlen("sites/"), - strlen(".php")); // cut those!
// or:
$dir = basename($filename, '.php');
// put the code listing files inside $dir
// then sort it (you did it in the first part)
// and then `return` the most or the least recent one
}, $listy),
嗯。我认为这是一个好办法。但我真的不知道如何在正确的代码中做到这一点。对不起,我还不是一个很好的程序员。我在代码中犯了很多简单的错误。
array_map(function ($filename) {
// $filename = sites/test1.php
$dir = substr($filename, strlen("sites/"), - strlen(".php")); // cut those!
// or:
$dir = basename($filename, '.php');
// put the code listing files inside $dir
// then sort it (you did it in the first part)
// and then `return` the most or the least recent one
}, $listy),