php下载远程zip文件到服务器,文件损坏几率特别大

业务里面需要一个从我的服务器下载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,下载完成后解压就会报错

clipboard.png

阅读 4.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题