php打包压缩文件并下载

// 批量下载文件
    public function LW_download()
    {
        header("Content-type: text/html; charset=utf-8");
        $moduleid=I("get.position");
        $Result=D('resume')->where(array('moduleid'=>$moduleid))->field('filename')->select();
 
        $array = array_column($Result,'filename');
        $tmpFile = tempnam('/Upload/temp', '');  //临时文件
        $zip = new \ZipArchive();  //php内置的压缩类
        $aa=$zip->open($tmpFile, \ZipArchive::CREATE);
     
        foreach ($array as $value) {
            $file=iconv("utf-8","gb2312",$url.$value); //,防止中文乱码   $url是存放目录
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 0);
            curl_setopt($ch, CURLOPT_URL, $file);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $fileContent = curl_exec($ch);
            curl_close($ch);
            $zip->addFromString(basename($file), $fileContent);  //将文件循环压缩到压缩包
        }
        $zip->close();
     
        header('Content-Type: application/zip');
        header ( "Content-Transfer-Encoding: Binary" );
        header("Content-disposition: attachment; filename='file.zip'");
        header('Content-Length: ' . filesize($tmpFile));  
        ob_end_clean();
        readfile($tmpFile);
        unlink($tmpFile);
        
    }
发布了36 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41965172/article/details/102909797