Php 获取最近添加到目录中的最新15个文件
假设有一个名为“abc”的目录 此目录包含多个文件。在所有这些文件中,我只想要php中数组中最新的“X”或最新的15个文件(如果可能的话,使用glob函数)Php 获取最近添加到目录中的最新15个文件,php,file,glob,Php,File,Glob,假设有一个名为“abc”的目录 此目录包含多个文件。在所有这些文件中,我只想要php中数组中最新的“X”或最新的15个文件(如果可能的话,使用glob函数) 每一个帮助都将非常有用。使用此处发布的功能: $dir=“/etc/php5/*”; //打开已知目录,然后继续读取其内容 foreach(全局($dir)作为$file) { echo“filename:$file:filetype:”.filetype($file)。“”; } 并在foreach循环中使用filetime()函数作为
每一个帮助都将非常有用。使用此处发布的功能:
$dir=“/etc/php5/*”;
//打开已知目录,然后继续读取其内容
foreach(全局($dir)作为$file)
{
echo“filename:$file:filetype:”.filetype($file)。“
”;
}
并在foreach循环中使用
filetime()
函数作为IF
语句。一种方法是使用
或者如果你还想用glob
$files = glob('folder/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
var_dump($file);
}
如果我错了,请改进我您尝试过什么吗?谢谢大家宝贵的时间和帮助。然而,我自己试过了,我也得到了答案,我将把它作为一个答案贴出来。这很有效。我知道这有点旧,但是你应该把代码放进标签里。
$dir = new \RecursiveDirectoryIterator('path/to/folder', \FilesystemIterator::SKIP_DOTS);
$it = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::SELF_FIRST);
$it->setMaxDepth(99); // search for other folders and they child folders
$files = [];
foreach ($it as $file) {
if ($file->isFile()) {
var_dump($file);
}
}
$files = glob('folder/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
var_dump($file);
}
// directory for searching files
$dir = "/etc/php5/*";
// getting files with specified four extensions in $files
$files = glob($dir."*.{extension1,extension2,extension3,extension4}", GLOB_BRACE);
// will get filename and filetime in $files
$files = array_combine($files, array_map("filemtime", $files));
// will sort files according to the values, that is "filetime"
arsort($files);
// we don't require time for now, so will get only filenames(which are as keys of array)
$files = array_keys($files);
$starting_index = 0;
$limit = 15;
// will limit the resulted array as per our requirement
$files = array_slice($files, $starting_index,$limit);
// will print the final array
echo "Latest $limit files are as below : ";
print_r($files);