1. 程式人生 > >遠端圖片打包下載

遠端圖片打包下載

$filename = date ( 'YmdH' ) . ".zip";
// 生成檔案
$zip = new \ZipArchive ();
// 使用本類,linux需開啟zlib,windows需取消php_zip.dll前的註釋
if ($zip->open ($filename ,\ZipArchive::OVERWRITE) !== true) {
//OVERWRITE 引數會覆寫壓縮包的檔案 檔案必須已經存在
if($zip->open ($filename ,\ZipArchive::CREATE) !== true){
// 檔案不存在則生成一個新的檔案 用CREATE開啟檔案會追加內容至zip
exit ( '無法開啟檔案,或者檔案建立失敗' );
}
}

    foreach ($imageData as $key =>$file) {
        $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);
        $info = pathinfo($file);
        $zip->addFromString(basename($key.'.'.$info['extension']), $fileContent);
    }
    $zip->close();
    //var_dump($filename);
    if(file_exists($filename)){
        ob_end_clean();
        header('Content-Description: File Download');
        header('Content-type: application.octet-stream');
        header('Content-Disposition: attachment; filename='.basename($filename));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        readfile($filename);
        exit;
    }