我相信在尤達提供的解決方案會工作,但我想(我認爲)一個更簡單的解決方案。
對於我而言,我需要保持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/)
你有沒有想過使用在'filename'返回'filemtime的'timestamp'()'日期並比較兩者? – yoda
嗯,所以我可以把壓縮文件的ZIP文件名創建時的時間戳。然後,根據ZIP的名稱檢查每個文件的filemtime()結果?是對的嗎?這似乎是可行的。謝謝。 – danbrellis