{"id":3534,"date":"2022-03-19T18:46:21","date_gmt":"2022-03-19T09:46:21","guid":{"rendered":"https:\/\/taitan916.info\/blog\/?p=3534"},"modified":"2024-01-13T14:08:23","modified_gmt":"2024-01-13T05:08:23","slug":"post-3534","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/3534","title":{"rendered":"PHP\u3067\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u307e\u3068\u3081\u3066zip\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u3001\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>PHP\u3067\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u307e\u3068\u3081\u3066zip\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u305f\u3044\u3002\u307e\u305f\u3001\u305d\u308c\u3068\u306f\u5225\u306b\u7279\u5b9a\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4e38\u3054\u3068zip\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u3001\u3053\u3061\u3089\u3082\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u305f\u3044\u3002\u4ee5\u4e0b\u306b2\u3064\u306e\u5bfe\u5fdc\u65b9\u6cd5\u3092\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092zip\u30d5\u30a1\u30a4\u30eb\u5316<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/zip\u5727\u7e2e\u3057\u305f\u3044\u30d5\u30a1\u30a4\u30eb\u4e00\u89a7\r\n$file_list = array(\r\n    '.\/index.html',\r\n    '.\/test.css',\r\n    '.\/test.js',\r\n    '.\/test.jpg',\r\n);\r\n\r\n\/\/zip\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4f5c\u6210\r\n$zip_obj = new zipArchive($file_list);\r\n\r\n\/\/\u4e00\u6642\u30d5\u30a1\u30a4\u30eb\u540d\u524d\u3068\u30d1\u30b9\r\n$zip_name = date('Ymdhis') . '.zip';\r\n$zip_path = '\/public_html\/' . $zip_name;\r\n\r\n\/\/\u4e00\u6642\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\r\n$result = $zip_obj-&gt;open($zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);\r\nif( $result !== true ){\r\n    echo 'error';\r\n    exit;\r\n}\r\n\r\n\/\/zip\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u8ffd\u52a0\r\nforeach ($file_list as $file) {\r\n    $file_name = basename($file);\r\n    $zip_obj-&gt;addFromString($file_name, file_get_contents($file));\r\n}\r\n$zip_obj-&gt;close();\r\n\r\n\/\/\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\r\nheader('Content-Type: application\/zip; name=\"' . $zip_name . '\"');\r\nheader('Content-Disposition: attachment; filename=\"' . $zip_name . '\"');\r\nheader('Content-Length: '.filesize($zip_path));\r\necho file_get_contents($zip_path);\r\n\r\n\/\/ \u4e00\u6642\u30d5\u30a1\u30a4\u30eb\uff08zip\u30d5\u30a1\u30a4\u30eb\uff09\u306e\u524a\u9664\r\nunlink($zip_path);\r\nexit();<\/pre>\n<p>\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u524d\u306e\u6642\u70b9\u307e\u3067\u306e\u51e6\u7406\u3067\u30e1\u30e2\u30ea\u6d88\u8cbb\u91cf\u3092\u6e2c\u5b9a\u3057\u305f\u3068\u3053\u308d\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u5408\u8a08\u30b5\u30a4\u30ba\u3068\u307b\u307c\u540c\u7b49\u306e\u30e1\u30e2\u30ea\u3092\u6d88\u8cbb\u3057\u3066\u3044\u305f\u3002\u30c6\u30b9\u30c8\u3057\u305f\u969b\u306f\u30d5\u30a1\u30a4\u30eb\u5408\u8a08\u30b5\u30a4\u30ba\u304c20MB\u3067\u30e1\u30e2\u30ea\u6d88\u8cbb\u91cf\u3082\u307b\u307c\u540c\u7b49\u3060\u3063\u305f\u3002<\/p>\n<p>\u753b\u50cf\u3084\u52d5\u753b\u30d5\u30a1\u30a4\u30eb\u3092\u3053\u306e\u65b9\u6cd5\u3067zip\u30d5\u30a1\u30a4\u30eb\u5316\u3059\u308b\u5834\u5408\u3001\u30e1\u30e2\u30ea\u6d88\u8cbb\u91cf\u304c\u3072\u3069\u3044\u3053\u3068\u306b\u306a\u308a\u305d\u3046\u3002\u305d\u306e\u969b\u306fphp.ini\u306ememory_limit\u306e\u5024\u306b\u6ce8\u610f\u3059\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u7279\u5b9a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4e38\u3054\u3068zip\u30d5\u30a1\u30a4\u30eb\u5316<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/zip\u5316\u3057\u305f\u3044\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\r\n$target_path = '\/public_html\/zip_dir';\r\n\r\n\/\/zip\u30d5\u30a1\u30a4\u30eb\u540d\r\n$zip_name = 'zip_dir.zip';\r\n\r\n\/\/zip\u30d5\u30a1\u30a4\u30eb\u306e\u4e00\u6642\u4fdd\u5b58\u5834\u6240\r\n$zip_path = '\/public_html\/' . $zip_name;\r\n\r\n\/\/zip\u30d5\u30a1\u30a4\u30eb\u4f5c\u6210\r\nchdir($target_path);\r\nexec(\"zip -r {$zip_path} .\");\r\n\r\n\/\/\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\r\nheader('Content-Type: application\/zip; name=\"' . $zip_name . '\"');\r\nheader('Content-Disposition: attachment; filename=\"' . $zip_name . '\"');\r\nheader('Content-Length: '.filesize($zip_path));\r\necho file_get_contents($zip_path);\r\n\r\n\/\/zip\u30d5\u30a1\u30a4\u30eb\u3092\u524a\u9664\r\nunlink($zip_path);\r\nexit();<\/pre>\n<p>\u3053\u3061\u3089\u3082\u30e1\u30e2\u30ea\u6d88\u8cbb\u91cf\u3092\u8a08\u6e2c\u3057\u305f\u3068\u3053\u308d\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u5408\u8a08\u30b5\u30a4\u30ba\u306f20MB\u306b\u5bfe\u3057\u3066\u30e1\u30e2\u30ea\u6d88\u8cbb\u91cf\u306f1KB\u672a\u6e80\u3060\u3063\u305f\u3002\u307e\u305f\u3001<a href=\"https:\/\/blog.ver001.com\/php-zip-archive\/#toc8\" target=\"_blank\" rel=\"noopener\">\u53c2\u8003\u30b5\u30a4\u30c8<\/a>\u306b\u3088\u308b\u3068\u51e6\u7406\u901f\u5ea6\u3082\u3053\u3061\u3089\u306e\u65b9\u6cd5\u306e\u65b9\u304c\u901f\u3044\u307f\u305f\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u6240\u611f<\/h2>\n<p>\u30e1\u30e2\u30ea\u6d88\u8cbb\u91cf\u3084\u51e6\u7406\u901f\u5ea6\u3092\u898b\u305f\u9650\u308a\u3067\u306f\u57fa\u672c\u7684\u306b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4e38\u3054\u3068zip\u30d5\u30a1\u30a4\u30eb\u5316\u3059\u308b\u306e\u304c\u826f\u3055\u305d\u3046\u3002\u305f\u3060\u3001<a href=\"https:\/\/taitan916.info\/blog\/archives\/3931\" target=\"_blank\" rel=\"noopener\">\u30ec\u30f3\u30bf\u30eb\u30b5\u30fc\u30d0\u7b49\u3067system\u95a2\u6570\u3084exec\u95a2\u6570\u304c\u4f7f\u7528\u3067\u304d\u306a\u3044\u30b1\u30fc\u30b9<\/a>\u3082\u3042\u308b\u306e\u3067\u3001\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u306f\u524d\u8ff0\u306e\u65b9\u6cd5\u3067\u5bfe\u5fdc\u3059\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u53c2\u8003\u30b5\u30a4\u30c8<\/h2>\n<p><a href=\"https:\/\/arts-factory.net\/zip\/\" target=\"_blank\" rel=\"noopener\">https:\/\/arts-factory.net\/zip\/<\/a><\/p>\n<p><a href=\"https:\/\/blog.ver001.com\/php-zip-archive\/\" target=\"_blank\" rel=\"noopener\">https:\/\/blog.ver001.com\/php-zip-archive\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u3067\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u307e\u3068\u3081\u3066zip\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u305f ... <\/p>\n","protected":false},"author":1,"featured_media":1085,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=3534"}],"version-history":[{"count":0,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3534\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1085"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=3534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=3534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=3534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}