![](/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.