0

トレーニングサイトに、データベースからIDが取得されたダウンロードをいくつか表示するページがあります。下部では、これらのファイルをすべてzip形式で圧縮してダウンロードできるようにコーディングできています。

ただし、ページが読み込まれるたびに、zipが再作成され、以前のzipファイルが上書きされます。ご想像のとおり、これにより、ロード時間が望ましいとは言えません。

最後のページの読み込み(最後のビューでページが開いた)以降にファイルが変更されていないかどうかをPHPでチェックし、変更されている場合は、zipを再作成して上書きする方法を知りたいです。

PHPのドキュメントを見ると、それと関係があるようfilemtime()ですが、私はそれについての経験がなく、Webサイトで使用できるかどうかさえわかりません。さらに詳しく調べてみると、私も経験のないキャッシングを行う必要があるのではないかと心配しています。

ヘルプ、提案、またはリードは非常に役立ちます。私がもっと意味をなすことができるか、または私の既存のコードのいずれかを背景として提供できるかどうか私に知らせてください。

4

2 に答える 2

0

インデックス作成の目的で、ここに答えを残します。

ファイルが最後に変更されたときに返さfilenameれた最後の日付と等しくなるようにを設定できるため、現在の日付とそれ自体を比較できます。衝突を避けるために、フォルダー構造をより複雑なものに分割する必要があるかもしれませんが、次のようなものにすることもできます。timestampfilemtime()timestampfilenamezipfilename

$timestamp = time();
$id = uniqid();
echo "Filename : {$timestamp}-{$id}.zip";
于 2012-06-21T18:16:34.580 に答える
0

Yoda が提供した解決策がうまくいったと確信していますが、私は (私が思うに) より簡単な解決策を選びました。

私の目的のために、zip ファイル名を一定に保つ必要がありました (ファイル名にタイムスタンプを入れることができませんでした)。代わりに、filemtime() を使用してファイルから最新のタイムスタンプを収集しました。次に、それらを zip ファイルのタイムスタンプと比較しました。zip の時間が最近のものではない場合、zip が存在しない場合、またはリスト内のファイルの数が zip 内の数と一致しない場合は、zip を再作成しました。それ以外の場合は、zip を表示しました。

wordpressプラグイン(Download Monitor)を介してダウンロード可能なファイルを保存および表示しているため、私の状況は少し独特です。基本的に私がやったことは次のとおりです。

/*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 / )

于 2012-07-03T16:52:53.753 に答える