php zip 不能创建文件,只有在文件发生变化的情况下,如何在PHP中创建ZIP?

我确信Yoda提供的解决方案可行,但我选择(我认为)更简单的解决方案。

出于我的目的,我需要保持zip文件名随时间不变(无法将时间戳放在文件名中)。相反,我使用filemtime()从文件中收集了最新的时间戳。然后我将它们与我的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.

}希望这可以帮助有类似问题的人。如果您对演示感兴趣,“培训库”中的几乎任何页面都会使用此代码(即http://chesapeakestormwater.net/training-library/all-about-stormwater/impervious-cover-and-stream-health/)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值