thinkphp3.2批量压缩文件并下载

public function LW_download()
    {
       
        $array = array('http://12.12.12.12/dgb/Public/Upload/01.jpg','http://12.12.12.12/dgb/Public/Upload/03.jpg','http://12.12.12.12/dgb/Public/Upload/02.jpg');
        $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);  //将文件循环压缩到压缩包
        }
        $zip->close();
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename='file.zip');
        header('Content-Length: ' . filesize($tmpFile));  
        readfile($tmpFile);
        unlink($tmpFile);
    }

linux要开启zlib扩展,路径要写全。

你可能感兴趣的:(thinkphp3.2批量压缩文件并下载)