php 3.2 生成压缩文件,并下载

    public function zip_download()
    {

        $array = array(
            'http://local.qki.com/site_upload/erweima/20190826/1566809174292_100063_865373044010119.png',
            'http://local.qki.com/site_upload/erweima/20190826/1566809222969_100064_865373044013253.png'
        );

        $tmpFile = tempnam('/temp', '');        //临时文件
        $zip = new \ZipArchive();                           //php内置的压缩类
        $zip->open($tmpFile, \ZipArchive::CREATE);

        foreach ($array as $value) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 0);
            curl_setopt($ch, CURLOPT_URL, $value);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $fileContent = curl_exec($ch);
            curl_close($ch);
            $zip->addFromString(basename($value), $fileContent);  //将文件循环压缩到压缩包
        }

        $filename = date('YmdHis',time()) . '_file.zip';
        $zip->close();
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $filename);
        header('Content-Length: ' . filesize($tmpFile));  
        readfile($tmpFile);
        unlink($tmpFile);
    }

 

posted @ 2019-08-27 18:18  盘思动  阅读(429)  评论(0编辑  收藏  举报