获取目录中最旧的日期文件排除文件 php


get date oldest file in directory exclude files php

我想获取目录中最旧文件的日期。我知道如何获取最旧的,但我希望它排除.png .jpg等文件。我尝试了这段代码,但它不起作用:

<?php 
$files = glob( 'test/*.*' );
$exclude_files = array('*.jpg', '*.bit', '*.png', '*.jpeg');
if (!in_array($files, $exclude_files)) {
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);
}
echo  date ("d F Y .", filemtime($files[0]));
?>

现在它得到了最旧文件的日期,但我希望它没有.jpg etx。

我该怎么做?

由于glob()会向您返回一个文件数组,因此您应该能够使用 array_filter() 过滤掉任何包含您不喜欢的扩展名的文件:

$files = array_filter(glob('test/*.*'), function($file) {
    // get the file's extension
    $ext = substr($file, strrpos($file, '.'));
    // check if the extension is in the list we don't want:
    return !in_array($ext, array('.jpg', '.bit', '.png', '.jpeg'));
});