Home > PHP Framework > Laravel > Create Zip compressed files in Laravel and provide downloadable code examples

Create Zip compressed files in Laravel and provide downloadable code examples

不言
Release: 2019-04-02 11:36:05
forward
3784 people have browsed it

The content of this article is about creating Zip compressed files in Laravel and providing code examples for downloading. It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

If you need your users to support multiple file downloads, the best way is to create a compressed package and provide it for download. Take a look at the implementation in Laravel.

In fact, this is not about Laravel, but more related to PHP. We are going to use the ZipArchive class that has existed since PHP 5.2. If you want to use it, you need to make surephp.ini# The ext-zip extension in ## is enabled.

Task 1: Store the user's invoice file to storage/invoices/aaa001.pdf

The following is the code display:

$zip_file = 'invoices.zip'; // 要下载的压缩包的名称

// 初始化 PHP 类
$zip = new \ZipArchive();
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);

$invoice_file = 'invoices/aaa001.pdf';

// 添加文件:第二个参数是待压缩文件在压缩包中的路径
// 所以,它将在 ZIP 中创建另一个名为 "storage/" 的路径,并把文件放入目录。
$zip->addFile(storage_path($invoice_file), $invoice_file);
$zip->close();

// 我们将会在文件下载后立刻把文件返回原样
return response()->download($zip_file);
Copy after login
The example is simple, right?

Task 2: Compress all files into the storage/invoices directory

No changes are required on the Laravel side, 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)
{
    // 我们要跳过所有子目录
    if (!$file->isDir()) {
        $filePath     = $file->getRealPath();

        // 用 substr/strlen 获取文件扩展名
        $relativePath = 'invoices/' . substr($filePath, strlen($path) + 1);

        $zip->addFile($filePath, $relativePath);
    }
}
$zip->close();
return response()->download($zip_file);
Copy after login
This is basically complete. You see, you don't need any Laravel extensions to implement this compression method.

【Related recommendations:

PHP video tutorial


##

The above is the detailed content of Create Zip compressed files in Laravel and provide downloadable code examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template