php批量打包下载文件的方法

直接上代码:

// 批量下载文件
    public function download()
    {
        header("Content-type: text/html; charset=utf-8");
    
        $ids=input("ids");

        $where['id']=array('in',$ids);
        $Result=db('apply')
        ->where($where)
        ->field('resume')->select();

        $array = array_column($Result,'resume');
        $tmpFile = tempnam('/Upload/temp', '');  //临时文件
        $zip = new \ZipArchive();  //php内置的压缩类
        $aa=$zip->open($tmpFile, \ZipArchive::CREATE);
     
        foreach ($array as $value) {
            $file=iconv("utf-8","gb2312","http://****/public/uploads/".$value);
            $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=".date('YmdHis').".zip");
        header('Content-Length: ' . filesize($tmpFile));  
        ob_end_clean();
        readfile($tmpFile);
        unlink($tmpFile);
        
    }
    function get_basename($filename){
        return preg_replace('/^.+[\\\\\\/]/', '', $filename);
       }
发布了36 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41965172/article/details/104843158
今日推荐