{"id":307,"date":"2005-05-24T12:53:16","date_gmt":"2005-05-24T11:53:16","guid":{"rendered":"http:\/\/wp.devco.net\/?p=307"},"modified":"2009-10-09T17:01:05","modified_gmt":"2009-10-09T16:01:05","slug":"creating_zip_files_with_php","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2005\/05\/24\/creating_zip_files_with_php.php","title":{"rendered":"Creating ZIP files with PHP"},"content":{"rendered":"

UPDATE:<\/B> You are better off using the library mentioned in this post<\/a>.<\/p>\n

As part of my previously mentioned OpenVPN CA I want to deliver keys, certs and config files to users in a single zip file that they can just extract onto their computers. PHP’s own ZIP File Functions<\/a> only supports reading zip files and not making them.<\/p>\n

Some Googling discovered an article<\/a> by John Coggeshall<\/a> that can create zip files. It does this by creating the binary data on the fly and can output the zip files directly to the browser from memory or by writing it to disk.<\/p>\n

I had some troubles getting hold of a usable version of this code since all these PHP code collection sites have this annoying habit of only showing the syntax highlighted versions of the code rather than give a download link. Eventually got one though and I figured I’ll host a mirror of it here<\/a> to help people out.<\/p>\n

Using it is very simple, this is a quick sample that will create a ZIP file and add one directory and one file into then send it directly to the client.<\/p>\n

\n
\n<?\nrequire (\"incl\/zipfile.inc.php\");\n$zipfile = new zipfile();\n$filedata = implode(\"\", file(\"incl\/zipfile.inc.php\"));\n$zipfile->add_dir(\"incl\/\");\n$zipfile->add_file($filedata, \"incl\/zipfile.inc.php\");\nheader(\"Content-type: application\/octet-stream\");\nheader(\"Content-disposition: attachment; filename=zipfile.zip\");\necho $zipfile->file();\n?>\n<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

UPDATE: You are better off using the library mentioned in this post. As part of my previously mentioned OpenVPN CA I want to deliver keys, certs and config files to users in a single zip file that they can just extract onto their computers. PHP’s own ZIP File Functions only supports reading zip files and […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[7],"tags":[121,42],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/307"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":1,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/307\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}