{"id":2607,"date":"2023-08-12T09:19:09","date_gmt":"2023-08-12T01:19:09","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2607"},"modified":"2023-08-12T09:19:09","modified_gmt":"2023-08-12T01:19:09","slug":"2607","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2607.html","title":{"rendered":"PHP\/ThinkPHP\u5b9e\u73b0\u6279\u91cf\u6253\u5305\u4e0b\u8f7d\u6587\u4ef6\u7684\u65b9\u6cd5\u793a\u4f8b"},"content":{"rendered":"

\u9700\u6c42\u63cf\u8ff0\uff1a<\/strong><\/span><\/p>\n

\u6709\u6570\u4e2a\u6587\u4ef6\uff0c\u5305\u542b\u56fe\u7247\uff0c\u6587\u6863\u3002\u9700\u8981\u6839\u636e\u6761\u4ef6\u81ea\u52a8\u6253\u5305\u6210\u538b\u7f29\u5305\uff0c\u63d0\u4f9b\u4e0b\u8f7d\u3002<\/p>\n

\u89e3\u51b3\uff08ZipArchive \u7c7b\uff09\uff1a<\/strong><\/span><\/p>\n

PHP\u63d0\u4f9b\u4e86ZipArchive \u7c7b\u53ef\u4e3a\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0cdemo\uff1a<\/p>\n

<?php\r\n \r\n$files = array('image.jpeg','text.txt','music.wav');\r\n$zipname = 'enter_any_name_for_the_zipped_file.zip';\r\n$zip = new ZipArchive;\r\n$zip->open($zipname, ZipArchive::CREATE);\r\nforeach ($files as $file) {\r\n $zip->addFile($file);\r\n}\r\n$zip->close();\r\n \r\n\/\/\/Then download the zipped file.\r\nheader('Content-Type: application\/zip');\r\nheader('Content-disposition: attachment; filename='.$zipname);\r\nheader('Content-Length: ' . filesize($zipname));\r\nreadfile($zipname);\r\n \r\n?><\/pre>\n

ThinkPHP\u7248<\/strong><\/p>\n

$zip = new \\ZipArchive;\r\n\/\/\u538b\u7f29\u6587\u4ef6\u540d\r\n$filename = 'download.zip';\r\n\/\/\u65b0\u5efazip\u538b\u7f29\u5305\r\n$zip->open($filename,\\ZipArchive::OVERWRITE);\r\n\/\/\u628a\u56fe\u7247\u4e00\u5f20\u4e00\u5f20\u52a0\u8fdb\u53bb\u538b\u7f29\r\nforeach ($images as $key => $value) {\r\n $zip->addFile($value);\r\n}\r\n\/\/\u6253\u5305zip\r\n$zip->close();\r\n \r\n\/\/\u53ef\u4ee5\u76f4\u63a5\u91cd\u5b9a\u5411\u4e0b\u8f7d\r\nheader('Location:'.$filename);\r\n \r\n\/\/\u6216\u8005\u8f93\u51fa\u4e0b\u8f7d\r\nheader("Cache-Control: public"); \r\nheader("Content-Description: File Transfer"); \r\nheader('Content-disposition: attachment; filename='.basename($filename)); \/\/\u6587\u4ef6\u540d \r\nheader("Content-Type: application\/force-download");\r\nheader("Content-Transfer-Encoding: binary");\r\nheader('Content-Length: '. filesize($filename)); \/\/\u544a\u8bc9\u6d4f\u89c8\u5668\uff0c\u6587\u4ef6\u5927\u5c0f \r\nreadfile($filename);<\/pre>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u9700\u6c42\u63cf\u8ff0\uff1a \u6709\u6570\u4e2a\u6587\u4ef6\uff0c\u5305\u542b\u56fe\u7247\uff0c\u6587\u6863\u3002\u9700\u8981\u6839\u636e\u6761\u4ef6\u81ea\u52a8\u6253\u5305\u6210\u538b\u7f29\u5305\uff0c\u63d0\u4f9b\u4e0b\u8f7d\u3002 \u89e3\u51b3\uff08ZipArchive […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2607","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2607"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2607"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2607\/revisions"}],"predecessor-version":[{"id":2608,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2607\/revisions\/2608"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}