使用PHP打包下載圖片,zip壓縮

public function productDownload()
{
    $id = intval(input('id', 0));
    $product = $this->opDataModel->setTable('photo_product')->where('id', $id)->find();
    if(empty($product)){
        return $this->output(0, '作品不存在');
    }

    $photoOrigin = $this->opDataModel->setTable('photo_image')->where('product_id', $id)->column('photo_origin');
    if(empty($photoOrigin)){
        return $this->output(0, '作品不存在圖片');
    }

    $tmpFile = tempnam(sys_get_temp_dir(), 'photo_');
    if(!$tmpFile){
        return $this->output(0, 'system error');
    }
     
    $zip = new \ZipArchive();
    $zip->open($tmpFile, \ZipArchive::CREATE);

    foreach($photoOrigin as $v){
        $fileContent = file_get_contents($v);
        $zip->addFromString(basename($v), $fileContent);
    }

    $zip->close();

    $out = "{$product['activity_id']}-{$product['id']}-".mb_substr($product['name'], 0, 10).'.zip';
     
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$out);
    header('Content-Length: ' . filesize($tmpFile));
    readfile($tmpFile);
    
    unlink($tmpFile);
    exit;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章