如何仅在文件发生更改时在PHP中创建ZIP?

如何仅在文件发生更改时在PHP中创建ZIP?,php,caching,ziparchive,Php,Caching,Ziparchive,我在一个培训网站上有一个页面,其中显示了几个从数据库中提取ID的下载。在底部,我已经成功地编写了代码,将所有这些文件压缩到zip中供下载 但是,每次加载页面时,都会重新创建zip并覆盖以前的zip文件。正如您可以想象的那样,这会导致一些加载时间不理想 我想知道如何进行PHP检查,以查看自上次页面加载(上次视图打开页面)以来是否有任何文件发生了更改,如果是,则重新创建zip并覆盖 通过查看PHP文档,似乎filemtime()。更深入地研究,我担心我可能需要涉及缓存,而我也没有这方面的经验 任何帮

我在一个培训网站上有一个页面,其中显示了几个从数据库中提取ID的下载。在底部,我已经成功地编写了代码,将所有这些文件压缩到zip中供下载

但是,每次加载页面时,都会重新创建zip并覆盖以前的zip文件。正如您可以想象的那样,这会导致一些加载时间不理想

我想知道如何进行PHP检查,以查看自上次页面加载(上次视图打开页面)以来是否有任何文件发生了更改,如果是,则重新创建zip并覆盖

通过查看PHP文档,似乎
filemtime()。更深入地研究,我担心我可能需要涉及缓存,而我也没有这方面的经验


任何帮助、建议或线索都会非常有用。让我知道我是否能讲得更有意义或提供我现有的任何代码作为背景。

出于指数化的目的,我将答案留在这里

您可以将
文件名
设置为等于上次更改文件时返回的
filemtime()
日期的最后一个
时间戳
,从而能够将当前的
时间戳
文件名
本身进行比较。它可能会迫使您将
zip
文件夹结构分解为更复杂的结构,以避免冲突,但您也可以将
filename
设置为类似以下内容:

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

我相信尤达提供的解决方案会奏效,但我选择了(我认为)更简单的解决方案

出于我的目的,我需要在一段时间内保持zip文件名不变(不能在文件名中放置时间戳)。相反,我使用filemtime()从文件中收集了最新的时间戳。然后,我将它们与zip文件的时间戳进行了比较。如果zip时间较短,zip不存在,或者列表中的文件数与zip中的文件数不匹配,我会重新创建zip,否则,我只显示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.
}
/*函数*/
//将url转换为绝对路径
函数url2path($url){
$parsed=parse_url($url);
如果(!is_array($parsed))返回false;
$path=$\u服务器['DOCUMENT\u ROOT']。$parsed['path'];
返回$path;
}
//返回最大的数字(适用于Unix时间戳)
函数get_最大_数($numbers){
如果(!is_array($numbers))返回false;
$highest=$number[0];
foreach($number作为$number)如果($highest<$number)$highest=$number;
返回$high;
}
$dtimes=array();
foreach($dl作为$d){//遍历我的下载数组
$dtimes[]=filemtime(url2path($d->filename));
//$dtimes是一个数组,包含我下载的所有unix时间戳
//显示个人下载等的其他代码
}
//邮政编码详细信息
$uploads=wp_upload_dir();
$parent=获取页面($post->post\u parent);
$zip_url=$uploads[baseurl]./downloads/zips/'.$parent->post_name.''.''.$post->post_name.'.zip';
如果($d次){
$latesttime=get_highest_number($dtimes);//文件的最新UNIX时间戳
//如果ZIP已经存在,则获取创建ZIP的时间戳
//我从页面标题创建ZIP文件名,并将它们全部存储在1个目录中,因此,我必须知道完整的ZIP文件名才能检索它。
如果($ziptime=filemtime(url2path($zip_url)){
$zip=新的ZipArchive();
$zip->open(url2path($zip_url));
//如果ZIP时间戳比文件最近,则显示ZIP
如果($ziptime>$latesttime&&$zip->numFiles==count($dtimes))$result=url2path($zip\u url);
else$result=cat有create\u zip($downloads,$zip\u url,true);
}
//如果ZIP不存在或ZIP时间戳不如文件最近,请创建/重写ZIP
else$result=create_zip($downloads,$zip_url,true);
//不管发生了什么,$result现在应该保留zip归档的路径。
}

希望这能帮助有类似问题的人。如果您对演示感兴趣,“培训库”中的几乎任何页面都使用此代码(即)

您是否想过在
文件名中使用返回的
filemtime()
日期的
时间戳
,并将两者进行比较?嗯,这样我就可以在ZIP的文件名中输入创建ZIP的时间戳。然后,对照ZIP的名称检查每个文件的filemtime()结果?是这样吗?这似乎是可行的。谢谢,好的,谢谢你的解释。我明天再做,然后带着结果回来。