2011-11-22 23:29

[PHP] 把多個圖片壓縮封裝並下載

<?php
$fileList = array(
    '/var/www/pics/001.jpg',
    '/var/www/pics/002.jpg',
    '/var/www/pics/003.jpg',
    '/var/www/pics/004.jpg',
    '/var/www/pics/005.jpg',
    '/var/www/pics/006.jpg',
);


/*建立臨時壓縮檔*/
$file = tempnam("tmp", "zip");
$zip = new ZipArchive;
$res = $zip->open($file, ZipArchive::CREATE|ZipArchive::OVERWRITE);
if ($res!==true) { exit('壓縮錯誤');}

foreach ($filePathList as $filePath){
    $zip->addFile($filePath, $fileName);
}
$zip->close();

ob_end_clean();
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename=pics_list.zip');

readfile($file);
unlink($file); 
exit;

參考連結:PHP: ZipArchive - Manual

0 回應: