{"id":1098,"date":"2023-09-07T15:09:11","date_gmt":"2023-09-07T15:09:11","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1098"},"modified":"2023-10-01T15:11:06","modified_gmt":"2023-10-01T15:11:06","slug":"create-zip-files-and-enable-download-functionality-in-laravel-9","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/create-zip-files-and-enable-download-functionality-in-laravel-9\/","title":{"rendered":"Create Zip Files and Enable Download Functionality in Laravel 9"},"content":{"rendered":"\n<p>File compression and archiving are common tasks in web development, often required to bundle multiple files into a single, downloadable archive. In Laravel 9, a powerful PHP framework, achieving this functionality is straightforward using the <code>ZipArchive<\/code> class. <\/p>\n\n\n\n<p>The code snippet you&#8217;ve provided is an example of how to create a zip file containing multiple files and offer it for download. Let&#8217;s dissect the code step by step.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function __invoke()\r\n{\r\n    $zip = new ZipArchive;\r\n\r\n    $fileName = 'myNewFile.zip';\r\n \r\n    if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)\r\n    {\r\n        $files = File::files(public_path('myFiles'));\r\n \r\n        foreach ($files as $key => $value) {\r\n            $relativeNameInZipFile = basename($value);\r\n            $zip->addFile($value, $relativeNameInZipFile);\r\n        }\r\n           \r\n        $zip->close();\r\n    }\r\n  \r\n    return response()->download(public_path($fileName));\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>Here&#8217;s what each part of the code does:<\/p>\n\n\n\n<p><strong><code>$zip = new ZipArchive;<\/code><\/strong>: This line initializes a new <code>ZipArchive<\/code> object, which provides the functionality to work with zip archives.<\/p>\n\n\n\n<p><strong><code>$fileName = 'myNewFile.zip';<\/code><\/strong>: Here, you define the name of the zip file you want to create. You can customize this to suit your needs.<\/p>\n\n\n\n<p><strong>Opening the Zip Archive<\/strong>: The <code>if<\/code> condition checks if the zip archive can be opened for writing. If successful, it proceeds to add files to the archive.<\/p>\n\n\n\n<p><strong>Adding Files to the Zip Archive<\/strong>: Inside the loop, the code iterates through the files in the <code>myFiles<\/code> directory and adds each file to the zip archive. It uses <code>addFile<\/code> to specify the source file and the relative path within the archive.<\/p>\n\n\n\n<p><strong>Closing the Zip Archive<\/strong>: After adding all the files, the zip archive is closed using the <code>close<\/code> method.<\/p>\n\n\n\n<p><strong><code>return response()-&gt;download(public_path($fileName));<\/code><\/strong>: Finally, the code returns a response to download the created zip file. The <code>public_path()<\/code> function is used to specify the file path.<\/p>\n\n\n\n<p><strong>Usage Example<\/strong><\/p>\n\n\n\n<p>To use this code, you can create a route or controller method that invokes the code when a specific URL is accessed. For instance, if you want users to download a zip file by visiting <code>example.com\/download-zip<\/code>, you can set up a route and controller for that URL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>File compression and archiving are common tasks in web development, often required to bundle multiple files into a single, downloadable archive. In Laravel 9, a powerful PHP&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1098","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1098"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1098\/revisions"}],"predecessor-version":[{"id":1099,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1098\/revisions\/1099"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}