0

已解决(忘记它是一个关联数组):

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);
    }
}
4

0 回答 0