php 實現一個字符串加密解密的函數實例代碼
702
2024-01-23
前言
本文主要給大家介紹的是關于PHP/ThinkPHP實現批量打包下載文件的相關內容,分享出來供大家參考學習,話不多說了,來一起看看詳細的介紹:
需求描述:
有數個文件,包含圖片,文檔。需要根據條件自動打包成壓縮包,提供下載。
解決(ZipArchive 類):
PHP提供了ZipArchive 類可為我們實現這一功能,demo:
<?php $files = array('image.jpeg','text.txt','music.wav'); $zipname = 'enter_any_name_for_the_zipped_file.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); ///Then download the zipped file. header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' . filesize($zipname)); readfile($zipname); ?>
ThinkPHP版
$zip = new ZipArchive; //壓縮文件名 $filename = 'download.zip'; //新建zip壓縮包 $zip->open($filename,ZipArchive::OVERWRITE); //把圖片一張一張加進去壓縮 foreach ($images as $key => $value) { $zip->addFile($value); } //打包zip $zip->close(); //可以直接重定向下載 header('Location:'.$filename); //或者輸出下載 header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.basename($filename)); //文件名 header("Content-Type: application/force-download"); header("Content-Transfer-Encoding: binary"); header('Content-Length: '. filesize($filename)); //告訴瀏覽器,文件大小 readfile($filename);
區別在引用的時候路徑要對,結束。
相關參考:
http://www.php.net/manual/zh/class.ziparchive.php
http://dengrongguan12.github.io/blog/2016/php-ziparchive/
總結
好了,大概就這樣,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對綠夏網的支持
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內容信息僅限用于學習和研究目的;不得將上述內容用于商業或者非法用途,否則,一切后果請用戶自負。本站信息來自網絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內,從您的電腦或手機中徹底刪除上述內容。如果您喜歡該程序或內容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯系進行刪除處理。敬請諒解!