业务里面需要一个从我的服务器下载zip文件到对方服务器的场景,但是下载的文件很多时候会文件损坏
我的下载代码:
function httpcopy($url,$file=''){
try {
set_time_limit(0);
touch($file);
// 做些日志处理
if ($fp = fopen($url, "rb")) {
if (!$download_fp = fopen($file, "wb")) {
exit;
}
while (!feof($fp)) {
if (!file_exists($file)) {
// 如果临时文件被删除就取消下载
fclose($download_fp);
exit;
}
fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8);
}
fclose($download_fp);
fclose($fp);
} else {
exit;
}
} catch (Exception $e) {
return ['status'=>false,'msg'=>$e->getMessage()];
}
return ['status'=>true,'msg'=>'success'];
}
打包是使用的PclZip,下载完成后解压就会报错