0

SOLVED (forgot it was an associative array):

foreach($list_s as $file => $timestamp) {
    echo $file;
    unlink($dir_files . $file);
    unlink($dir_sql . $file);
}

I have a backup area and I want every time the user visits it for it to run a code that only keeps n (user specified amount of backups to be kept) numbers of the newest backups.

I am pretty sure I have the underlying logic correct with array_slice. But the code does not delete the files, and I cannot pin-point where the issue is.

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);
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alex
  • 9,215
  • 8
  • 39
  • 82
  • just for debugging you can `print_r($list_s);` before `foreach` and `echo $file` inside foreach loop –  Jul 14 '13 at 01:13
  • `Array ( [dbbackup_14.07.2013_00_50_23.sql] => 1373763023 [dbbackup_14.07.2013_00_49_40.sql] => 1373762980 ) 13737630231373762980` the file appears to be the unix timestamp but im not sure why! – Alex Jul 14 '13 at 01:14
  • ok so, you can now deal with it and find the issue –  Jul 14 '13 at 01:17

0 Answers0