![](/img/trans.png)
[英]How do I sort a directory by time, and display the last four image files matching a pattern with php?
[英]How to delete files matching pattern within a directory in PHP?
我创建了一个PHP脚本来生成CSV文件。 我想只保留脚本创建的最新文件。 如何使用PHP删除目录中的所有旧*.csv
文件?
// Get a list of all CSV files in your folder.
$csv = glob("*.csv");
// Sort them by modification date.
usort($csv, function($a, $b) { return filemtime($a) - filemtime($b); });
// Remove the newest from your list.
array_pop($csv);
// Delete all the rest.
array_map('unlink', $csv);
如果您的CSV扩展名可能是任何情况,请将csv
与[cC][sS][vV]
交换。 这是一个glob模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.