php zip打包文件批量下載

php zip打包文件批量下載

$zipname = 'file.zip';
//file_path爲文件位置,這裏用的是thinkphp,路徑爲相對於入口文件index.php的位置,
//down_path爲打包後文件的文件名
if(file_exists($res['zpstr1'])){
	$fileArr[0] = array('file_path' => './1.jpg', 'down_path' => '1.jpg');
}
if(file_exists($res['zpstr2'])){
	$fileArr[1] = array('file_path' => './2.jpg', 'down_path' => '2.jpg');
}
if($fileArr != null){
$zip = new \ZipArchive ();
$res = $zip->open ( $zipname, \ZipArchive::CREATE );
if ($res === TRUE) {
    foreach ( $fileArr as $file ) {

        //這裏將服務器上的文件添加到下載內容中,並重新賦值下載zip文件內該文件的路徑
        $zip->addFile ( $file ['file_path'], $file ['down_path'] );

    }
}
$zip->close ();
header ( "Content-Type: application/zip" );
header ( "Content-Transfer-Encoding: Binary" );
header ( "Content-Length: " . filesize ( $zipname ) );
header ( "Content-Disposition: attachment; filename=\"" . basename ( $zipname ) . "\"" );

readfile ( $zipname );

//如不刪除,則在服務器上會有 $zipname 這個zip文件
@unlink ( $zipname );
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章