where('id', intval($productId))->firstOrFail(); /** @var \Illuminate\Filesystem\FilesystemAdapter */ $exportDisk = Storage::disk('export'); if (!($exportDisk instanceof \Illuminate\Filesystem\FilesystemAdapter)) { throw new \Error('Storage disk export has to be local file system'); } if (!$exportDisk->directoryExists('export-product')) { $exportDisk->createDirectory('export-product'); } // * Create zip $zip = new \ZipArchive(); $fileName = date('d-m-Y') . '-' . Str::uuid() . '.zip'; if (!$zip->open($exportDisk->path("export-product/{$fileName}"), \ZipArchive::CREATE) === true) { throw new \RuntimeException("The ZIP file cannot be created."); } $dirName = Str::slug( Carbon::parse($product->updated_at)->format('y-m-d') . '-' . $product->user->lastname . '-' . $product->user->id ); if (!$zip->addEmptyDir($dirName)) { throw new \RuntimeException("The folder cannot be created inside the ZIP."); } // * Add files $pdf = Product::generateProductPdf($product, "test"); $exportDisk->put('pdf/' . $product->id . '.pdf', $pdf->output()); $zip->addFile($exportDisk->path('pdf/' . $product->id . '.pdf'), $dirName . "/" . $product->name . ".pdf"); $zip->addFile($product->picture, $dirName . "/photo." . pathinfo($product->picture, PATHINFO_EXTENSION)); foreach ($product->docs as $doc) { $zip->addFile($doc, $dirName . "/document." . pathinfo($doc, PATHINFO_EXTENSION)); } // * Save zip $zip->close(); if (!$exportDisk->exists("export-product/{$fileName}")) { throw new \RuntimeException("The ZIP file 'export-product/{$fileName}' has not been created."); } Product::downloadPrivateExportFile("export-product/{$fileName}"); } }