laravel 打包成zip並下載

		//初始化zip 名字
 		$zip_file = 'Example.zip';
        $zip = new \ZipArchive();
        $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
        //將被壓縮文件夾
        $path = storage_path('/app/files/');
        $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);
發佈了21 篇原創文章 · 獲贊 8 · 訪問量 5371
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章