2013-10-24 40 views
0

我一直是這樣的代碼工作了一整天之後,但計時器已用完之後由於某種原因,文件不會被刪除。$ zip_file_name的是,我想以後30secs看到了文件PHP自動刪除文件定時器

$the_folder = '../test'; 
$zip_file_name = ("PHOENIX_fullbackup_$now.zip"); 


$download_file= true; 
//$delete_file_after_download= true; doesnt work!! 


class FlxZipArchive extends ZipArchive { 
    /** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;; @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private **/ 

    public function addDir($location, $name) { 
     $this->addEmptyDir($name); 

     $this->addDirDo($location, $name); 
    } // EO addDir; 

    /** Add Files & Dirs to archive;;;; @param string $location Real Location; @param string $name Name in Archive;;;;;; @author Nicolas Heimann 
    * @access private **/ 
    private function addDirDo($location, $name) { 
     $name .= '/'; 
     $location .= '/'; 

     // Read all Files in Dir 
     $dir = opendir ($location); 
     while ($file = readdir($dir)) 
     { 
      if ($file == '.' || $file == '..') continue; 
      // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File(); 
      $do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile'; 
      $this->$do($location . $file, $name . $file); 
     } 
    } // EO addDirDo(); 
} 

$za = new FlxZipArchive; 
$res = $za->open($zip_file_name, ZipArchive::CREATE); 
if($res === TRUE) 
{ 
    $za->addDir($the_folder, basename($the_folder)); 
    $za->close(); 
} 
else { echo 'Could not create a zip archive';} 

if ($download_file) 
{ 
    ob_get_clean(); 
    header("Pragma: public"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private", false); 
    header("Content-Type: application/zip"); 
    header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: " . filesize($zip_file_name)); 
    readfile($zip_file_name); 


    if ($handle = opendir ($zip_file_name)) { 

    while (false !== ($file = readdir($handle))) { 
     if (filectime($file)< (time()-30)) { // timer -30secs 
      unlink($file); 
     } 
    } 
    } 

} 
?> 

任何幫助是極大appreciated.thanks

+0

爲什麼30秒? – deceze

+0

似乎足夠,因爲該網站是在LAN.thanks – Carter

+0

下載完成後腳本停下來,從未達到30秒。您必須設置一個cron作業,定期檢查是否有超過30秒的文件並刪除它們。 – Reeno

回答

0

你可以生成壓縮文件之前刪除的zip文件(如果它已經存在) - 這樣就會刪除舊文件中生成新的第一前:

Eg (你的代碼片段)

<?php 

// Delete previous zip files 
$old_files = glob($the_folder .'/*.zip'); 
foreach ($old_files as $old_file) { 
    unlink($old_file); 
} 

// Usage 
$za = new FlxZipArchive; 
$res = $za->open($zip_file_name, ZipArchive::CREATE); 
if ($res === TRUE) 
{ 
    // Add Files to Zip 
    $za->addDir($the_folder, basename($the_folder)); 
    $za->close(); 

    // Download If enabled 
    if ($download_file) 
    { 
     ob_get_clean(); 
     header("Pragma: public"); 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Cache-Control: private", false); 
     header("Content-Type: application/zip"); 
     header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";"); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: " . filesize($zip_file_name)); 
     readfile($zip_file_name); 
    } 
} 
else 
{ 
    echo 'Could not create a zip archive'; 
} 

?> 
+0

多數民衆贊成它感謝Latheesan Kanes ..thats真棒作品完美。我可以在$ zip_file_name上使用通配符,因爲$ now = date(「Ymd」)。因此,如果用戶在$ zip_file_name不會相同的2天后回來。再次感謝 – Carter

+0

我已經更新了我的答案。該腳本現在將進入'$ the_folder'中指定的任何目錄,並查找所有擴展名爲「.zip」的文件並刪除它們。 – Latheesan

+0

輝煌的作品像一個魅力。再次感謝。 – Carter