php打包压缩下载多大,php多文件打包压缩下载简单示例
調用php中內置擴展ZipArchive類可以進行文件壓縮打包下載,(PHP 5>=5.2.0,PHP 7,PECL zip>=1.1.0)
調用php中內置擴展ZipArchive類可以進行文件壓縮打包下載,(PHP?5>=5.2.0,PHP?7,PECL?zip>=1.1.0)
簡單示例:<?php
function zipfile($file_path){
// 壓縮多個文件
$fileList = explode(',', $file_path);
$filename = time() . ".zip"; // 壓縮包所在的位置路徑
$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE); //打開壓縮包
foreach ($fileList as $file) {
$zip->addFile($file, basename($file)); //向壓縮包中添加文件
}
$rs = $zip->close(); //關閉壓縮包
//下載文件
$file = fopen($filename, "r");
//返回的文件類型
Header("Content-type: application/octet-stream");
//按照字節大小返回
Header("Accept-Ranges: bytes");
//返回文件的大小
Header("Accept-Length: " . filesize($filename));
//這里設置客戶端的彈出對話框顯示的文件名
Header("Content-Disposition: attachment; filename=" . $filename);
//一次性將數據傳輸給客戶端
//echo fread($file, filesize($filePath));
//一次只傳輸1024個字節的數據給客戶端
//向客戶端回送數據
$buffer = 1024;//
//判斷文件是否讀完
while (!feof($file)) {
//將文件讀入內存
$file_data = fread($file, $buffer);
//每次向客戶端回送1024個字節的數據
echo $file_data;
}
//將生成的zip文件在服務器端刪除,只需要客戶端下載就行了
@unlink($filename);
}
zipfile('E:\www\test\filetest\getfile\1.jpg,E:\www\test\filetest\getfile\1.txt');
?>
ZipArchive::addEmptyDir?—?添加新目錄
ZipArchive::addFile?—?將文件從給定路徑添加到ZIP存檔
ZipArchive::addFromString?—?使用文件的內容將文件添加到ZIP存檔
ZipArchive::addGlob?—?按glob模式從目錄添加文件
ZipArchive::addPattern?—?按PCRE模式從目錄添加文件
ZipArchive::close?—?關閉活動存檔(已打開或新創建)
ZipArchive::count?—?統計存檔中的文件數
ZipArchive::deleteIndex?—?使用存檔中的索引刪除該項
ZipArchive::deleteName?—?刪除存檔中使用其名稱的條目
ZipArchive::extractTo?—?提取存檔內容
ZipArchive::getArchiveComment?—?返回Zip存檔注釋
ZipArchive::getCommentIndex?—?使用條目索引返回條目的注釋
ZipArchive::getCommentName?—?返回使用項名稱的項的注釋
ZipArchive::getExternalAttributesIndex?—?檢索由其索引定義的項的外部屬性
ZipArchive::getExternalAttributesName?—?檢索由其名稱定義的項的外部屬性
ZipArchive::getFromIndex?—?使用其索引返回項內容
ZipArchive::getFromName?—?使用其名稱返回條目內容
ZipArchive::getNameIndex?—?返回使用其索引的項的名稱
ZipArchive::getStatusString?—?返回狀態錯誤消息、系統和/或zip消息
ZipArchive::getStream?—?獲取由其名稱定義的條目的文件處理程序(只讀)
ZipArchive::isCompressionMethodSupported?—?檢查libzip是否支持壓縮方法
ZipArchive::isEncryptionMethodSupported?—?檢查libzip是否支持加密方法
ZipArchive::locateName?—?返回存檔項的索引
ZipArchive::open?—?打開壓縮文件存檔
ZipArchive::registerCancelCallback?—?注冊回調以允許在存檔關閉期間取消。
ZipArchive::registerProgressCallback?—?注冊回調以在存檔關閉期間提供更新。
ZipArchive::renameIndex?—?重命名由其索引定義的項
ZipArchive::renameName?—?重命名由其名稱定義的項
ZipArchive::replaceFile?—?用給定的路徑替換ZIP存檔中的文件
ZipArchive::setArchiveComment?—?設置ZIP存檔的注釋
ZipArchive::setCommentIndex?—?設置由其索引定義的項注釋
ZipArchive::setCommentName?—?設置由名稱定義項注釋
ZipArchive::setCompressionIndex?—?設置由索引定義的項的壓縮方法
ZipArchive::setCompressionName?—?設置由名稱定義的項的壓縮方法
ZipArchive::setEncryptionIndex?—?設置由其索引定義的項的加密方法
ZipArchive::setEncryptionName?—?設置由名稱定義的項的加密方法
ZipArchive::setExternalAttributesIndex?—?設置由其索引定義的項的外部屬性
ZipArchive::setExternalAttributesName?—?設置由名稱定義的項的外部屬性
ZipArchive::setMtimeIndex?—?設置由其索引定義的項的修改時間
ZipArchive::setMtimeName?—?設置由名稱定義的條目的修改時間
ZipArchive::setPassword?—?設置活動存檔的密碼
ZipArchive::statIndex?—?獲取由其索引定義的項的詳細信息
ZipArchive::statName?—?獲取由名稱定義的項的詳細信息
ZipArchive::unchangeAll?—?撤消存檔中所做的所有更改
ZipArchive::unchangeArchive?—?還原存檔中完成的所有全局更改
ZipArchive::unchangeIndex?—?還原對給定索引項所做的所有更改
ZipArchive::unchangeName?—?還原對具有給定名稱的條目所做的所有更改
總結
以上是生活随笔為你收集整理的php打包压缩下载多大,php多文件打包压缩下载简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse导入项目pom文件报错_e
- 下一篇: python数学公式pdf文件的转换_p