...

Learn how to create a ZIP file of selected category images in Laravel

Efficiently Download Selected Categories as a ZIP File in Laravel.This post demonstrates how to implement a feature in Laravel to compress selected category images into a ZIP file and download it. With dynamic file handling and error management, this feature is robust and user-friendly.

Steps to Implement

  1. Set Up the Method in Your Controller
    Add the downloadSelected method to handle the download functionality. It fetches selected categories, compresses their associated images, and serves them as a downloadable ZIP.

  2. Ensure Storage Path and File Handling

    • Use storage_path('app/public') to define a directory where the ZIP file will be stored temporarily.
    • Verify directory existence with File::exists() and create it if necessary.
  3. Fetch and Validate Selected Rows
    Retrieve the selected categories using their IDs. Ensure each image exists before adding it to the ZIP archive.

  4. Create a ZIP Archive

    • Use PHP’s ZipArchive class to dynamically create a ZIP file.
    • Handle file paths and ensure only valid files are included.
  5. Return ZIP File as Response
    Serve the ZIP file to the user with response()->download() and delete it after download to free up storage.

  6. Handle Errors Gracefully
    Implement error handling using try-catch blocks, with logs for troubleshooting and user-friendly alerts for better UX.


 public $selectAll = false;

    // Seleted toggle
    public function selectAllToggle()
    {
        if ($this->selectAll) {
            $this->selectedRows = $this->category->pluck('id')->map(function ($id) {
                return (string) $id;
            })->toArray();
        } else {
            $this->selectedRows = [];
        }
    }



    public function downloadSelected()
    {
        try {
            if (empty($this->selectedRows)) {
                $this->alert('warning', 'No categories selected for download.');
                return;
            }

            // File name and path
            $zipFileName = 'home_categories_' . now()->timestamp . '.zip';
            $directory = storage_path('app/public'); // Store in public directory
            // Ensure the directory exists
            if (!File::exists($directory)) {
                File::makeDirectory($directory, 0755, true);
            }

            $zipFilePath = "{$directory}/{$zipFileName}";

            // Create a ZIP file
            $zip = new ZipArchive;

            if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
                foreach ($this->selectedRows as $categoryId) {
                    $category = HomeCategory::find($categoryId);
                    if ($category && $category->image) {
                        $imagePath = storage_path("app/public/portfolio/home_category/{$category->image}");
                        if (File::exists($imagePath)) {
                            $zip->addFile($imagePath, basename($imagePath));
                        } else {
                            Log::warning("Image file does not exist: {$imagePath}");
                        }
                    }
                }
                $zip->close();

                // Return the ZIP file for download
                return response()->download($zipFilePath)->deleteFileAfterSend(true);
            } else {
                Log::error("Failed to create ZIP file at {$zipFilePath}");
                $this->alert('error', 'Failed to create ZIP file.');
            }
        } catch (\Exception $e) {
            Log::error('Download Error: ' . $e->getMessage());
            $this->alert('error', 'An error occurred while downloading: ' . $e->getMessage(), [
                'toast' => false,
                'position' => 'center',
                'showConfirmButton' => true,
                'allowOutsideClick' => false,
                'timer' => null
            ]);
        }
    }

William Anderson

I am a versatile Full-Stack Web Developer with a strong focus on Laravel, Livewire, Vue.js, and Tailwind CSS. With extensive experience in backend development, I specialize in building scalable, efficient, and high-performance web applications.