In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Laravel to create Zip files and achieve download", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use Laravel to create Zip files and achieve download" bar!
Create Zip compressed files in Laravel and provide downloads
If you need your users to support multiple file downloads, the best way is to create a zip file and provide it for download. Take a look at the implementation in Laravel.
In fact, this is not about Laravel, but more about PHP. We are going to use the ZipArchive class that has existed since PHP 5.2, and if we want to use it, we need to make sure that the ext-zip extension in php.ini is turned on.
Task 1: store the user's invoice file to storage/invoices/aaa001.pdf
The following is a code demonstration:
$zip_file = 'invoices.zip'; / / name of the compressed package to be downloaded / / initialize the PHP class $zip = new\ ZipArchive (); $zip- > open ($zip_file,\ ZipArchive::CREATE |\ ZipArchive::OVERWRITE); $invoice_file =' invoices/aaa001.pdf' / / add a file: the second parameter is the path of the file to be compressed in the package / / so it will create another path called "storage/" in ZIP and put the file in the directory. $zip- > addFile (storage_path ($invoice_file), $invoice_file); $zip- > close (); / / We will return the file as it is immediately after downloading the file, return response ()-> download ($zip_file)
The example is simple, isn't it?
Task 2: compress all files into the storage/invoices directory
There is no need to change anything in the Laravel aspect, we just need to add some simple PHP code to iterate over these files.
$zip_file = 'invoices.zip';$zip = new\ ZipArchive (); $zip- > open ($zip_file,\ ZipArchive::CREATE |\ ZipArchive::OVERWRITE); $path = storage_path (' invoices'); $files = new\ RecursiveIteratorIterator (new\ RecursiveDirectoryIterator ($path)); foreach ($files as $name = > $file) {/ / We want to skip all subdirectories if (! $file- > isDir ()) {$filePath = $file- > getRealPath () / / get the file extension $relativePath = 'invoices/'. Substr ($filePath, strlen ($path) + 1); $zip- > addFile ($filePath, $relativePath);}} $zip- > close (); return response ()-> download ($zip_file)
It's almost done here. You see, you don't need any Laravel extension packs to implement this compression.
Thank you for your reading, the above is "how to use Laravel to create Zip files and achieve download" content, after the study of this article, I believe you on how to use Laravel to create Zip files and achieve download this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.