Skip to content

Instantly share code, notes, and snippets.

@HelgeSverre
Last active November 8, 2016 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelgeSverre/bc9446238cb24a5efe80d695db7d7a7a to your computer and use it in GitHub Desktop.
Save HelgeSverre/bc9446238cb24a5efe80d695db7d7a7a to your computer and use it in GitHub Desktop.
<?php
// imagine $imageIds is a get parameter with a comma seperated string of ID's
// Eample: http://localhost/download-zip?images=1,2,3,4
Route::get("/download-zip", function ($images) {
$imageIds = explode(",", $images);
// This is an eloquent model
$images = Images::find($imageIds);
if (!$images) {
return Response::make("No images found", 404);
}
$zipFileName = tempnam("tmp", "zip");
// Open zip file
$zip = (new ZipArchive())->open($zipFileName;, ZipArchive::OVERWRITE);
// Add each file
foreach ($images as $image) {
$zip->addFromString(image->filename, base64_decode($$image->base64_string));
}
// Close the file
$zip->close();
// Return the file response
return response()->file($zipFileName);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment