2013-10-24 38 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