zip下載

function download_img_all(){

   //圖片數組
    $res = [
       [
         'img_path'=>'a.png',
       ],
       [
         'img_path'=>'b.png',
       ]
    ];
    //建立壓縮包的路徑
    $filename = $_SERVER['DOCUMENT_ROOT'].'/Dwonload.zip';
    $zip = new \ZipArchive;
    // $zip->deleteIndex();
    $zip->open($filename,$zip::CREATE);
    //往壓縮包內添加目錄
    $zip->addEmptyDir('images'); 
    foreach ($res as $value) {
         $fileData = file_get_contents($_SERVER['DOCUMENT_ROOT']."/".$value['img_path']);
        if ($fileData) {
            $add = $zip->addFromString('images/'.$value['img_path'], $fileData);
        }
    }
    $zip->close();
    //打開文件
   
    //下載文件
    ob_end_clean();
    header("Content-Type: application/force-download");
    header("Content-Transfer-Encoding: binary");
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename='.time().'.zip');
    header('Content-Length: '.filesize($filename));
    error_reporting(0);
    readfile($filename);
    flush();
    ob_flush();


}

echo download_img_all();
相關文章
相關標籤/搜索