How to save download zip to public folder in laravel?

by muriel.schmidt , in category: PHP Frameworks , 4 months ago

How to save download zip to public folder in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , 4 months ago

@muriel.schmidt 

To save a downloaded zip file to the public folder in Laravel, you can follow the following steps:

  1. Ensure that you have the necessary zip file download functionality in your controller or route.
  2. Use the Storage facade in Laravel to store the downloaded zip file in the public folder. Here's an example code snippet to save a zip file to the public folder:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use IlluminateSupportFacadesStorage;

public function downloadZipFile()
{
    // Download the zip file here
    $zipContents = // Fetch or create the zip file contents

    // Save the zip file to the public folder
    Storage::disk('public')->put('file.zip', $zipContents);

    // Return a download response or redirect to the stored zip file
}


In the above example, replace $zipContents with the actual contents of the zip file you want to save and file.zip with the desired filename for the stored zip file.

  1. Make sure the public disk is configured properly in your config/filesystems.php file. The default configuration for the public disk should look like this:
1
2
3
4
5
6
'public' => [
    'driver' => 'local',
    'root' => public_path(),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],


  1. After saving the zip file in the public folder, you can provide a download link to the saved file or directly serve it to the users as needed.


By following these steps, you should be able to save a downloaded zip file to the public folder in Laravel.