已解决(忘记是关联数组):
foreach($list_s as $file => $timestamp) {
echo $file;
unlink($dir_files . $file);
unlink($dir_sql . $file);
}
我有一个备份区域,我希望每次用户访问它时都要运行一个代码,该代码只保留n
(用户指定的备份数量)最新备份的数量。
我很确定我的基础逻辑正确array_slice
。但代码不会删除文件,我无法确定问题的位置。
function listdir_by_date($dir)
{
$h = opendir($dir);
$_list = array();
while ($file = readdir($h)) {
if ($file != '.' and $file != '..') {
$ctime = filectime($dir . $file);
$_list[$file] = $ctime;
}
}
closedir($h);
// reorder: associative array in asc order
ksort($_list);
return $_list;
}
$sql = Nemesis::select('backup_amt', 'config');
list($backup_amt) = $sql->fetch_row();
$list = listdir_by_date($dir_sql);
$file = readdir($dir_sql);
if (count($list) > $backup_amt && is_numeric($backup_amt)) {
/* we delete the user specified backup by the backup amt
say we have 5, user specified only 2 backups to be kept
we delete the last 3 backups */
$list_s = array_slice($list, 0, count($list) - $backup_amt);
foreach ($list_s as $file => $timestamp) {
@unlink($dir_files . $file);
@unlink($dir_sql . $file);
}
}