2012-06-21 21 views
0

我在一個培訓網站上有一個頁面,顯示從數據庫中提取的幾個下載的ID。最後,我已經成功編碼,將所有這些文件壓縮成一個zip文件供下載。如何僅在文件發生更改時纔在PHP中創建ZIP?

然而,每次頁面加載時,拉鍊被重建,並覆蓋前一個zip文件。正如你可以想象的那樣,這會導致一些負載時間不夠理想。

我想知道如何使PHP檢查,看看自上次頁面加載(最後一個視圖打開頁面)以來是否有任何文件已更改,如果是,則重新創建zip並覆蓋。

尋找PHP文檔,似乎filemtime()可能與它有關,但我沒有經驗,甚至不知道我可以在網站上使用它。仔細觀察它,我擔心我可能需要涉及緩存,而我也沒有經驗。

任何幫助,建議或線索將是非常有益的。讓我知道,如果我可以更有意義或提供任何我現有的代碼作爲背景。

+2

你有沒有想過使用在'filename'返回'filemtime的'timestamp'()'日期並比較兩者? – yoda

+1

嗯,所以我可以把壓縮文件的ZIP文件名創建時的時間戳。然後,根據ZIP的名稱檢查每個文件的filemtime()結果?是對的嗎?這似乎是可行的。謝謝。 – danbrellis

回答

0

我相信在尤達提供的解決方案會工作,但我想(我認爲)一個更簡單的解決方案。

對於我而言,我需要保持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/

0

對於指數而言,我在這裏留下了答案。

您可以設置filename等於返回filemtime()日期的最後timestamp在文件的最後修改,從而能夠比較當前timestampfilename本身。這可能會迫使你打破zip文件夾結構到更復雜的東西,以避免colisions,但話又說回來,你還可以使filename類似:

$timestamp = time(); 
$id = uniqid(); 
echo "Filename : {$timestamp}-{$id}.zip"; 
+0

好的,謝謝你的拼寫。我明天會努力工作,並拿回我的結果。 – danbrellis

相關問題