我确信 Yoda 提供的解决方案会奏效,但我选择了(我认为)更简单的解决方案。
出于我的目的,我需要随着时间的推移保持 zip 文件名不变(无法将时间戳放在文件名中)。相反,我使用 filemtime() 从文件中收集了最新的时间戳。然后我将它们与我的 zip 文件的时间戳进行了比较。如果压缩时间较新,压缩不存在,或者列表中的文件数量与压缩中的数量不匹配,我重新创建压缩,否则,我只显示压缩。
我的情况有点独特,因为我通过 wordpress 插件(下载监视器)存储和显示可下载文件。这基本上是我所做的:
/*Functions*/
//Convert url to absolute path
function url2path($url){
$parsed = parse_url($url);
if(!is_array($parsed)) return false;
$path = $_SERVER['DOCUMENT_ROOT'].$parsed['path'];
return $path;
}
//Returns the highest number (works with Unix Timestamps)
function get_highest_number($numbers){
if(!is_array($numbers)) return false;
$highest = $numbers[0];
foreach($numbers as $number) if($highest < $number) $highest = $number;
return $highest;
}
$dtimes = array();
foreach($dl as $d) { //iterate through my array of downloads
$dtimes[] = filemtime(url2path($d->filename));
//$dtimes is an array containing all the unix timestamps of my downloads
//other code to display individual downloads, etc
}
//Zip Details
$uploads = wp_upload_dir();
$parent = get_page($post->post_parent);
$zip_url = $uploads[baseurl].'/downloads/zips/'.$parent->post_name.'_'.$post->post_name.'.zip';
if($dtimes){
$latesttime = get_highest_number($dtimes); //latest UNIX timestamp of files
//If ZIP already exists, get timestamp of when ZIP was created
//I create the ZIP file name from the page title and store them all in 1 directory, thus, I would have to know the full zip filename to retrieve it.
if($ziptime = filemtime(url2path($zip_url))){
$zip = new ZipArchive();
$zip->open(url2path($zip_url));
//If ZIP timestamp is more recent than file, show ZIP
if($ziptime > $latesttime && $zip->numFiles == count($dtimes)) $result = url2path($zip_url);
else $result = cat has create_zip($downloads,$zip_url,true);
}
//If ZIP doesn't exist or ZIP timestampe is less recent than files, create/rewrite ZIP
else $result = create_zip($downloads,$zip_url,true);
//regardless of what has happened, $result should now hold the path to the zip archive.
}
希望这可以帮助有类似问题的人。如果您对演示感兴趣,“培训库”中的几乎所有页面都使用此代码(即http://chesapeakestormwater.net/training-library/all-about-stormwater/impervious-cover-and-stream-health / )