Last active
November 8, 2016 12:17
-
-
Save HelgeSverre/bc9446238cb24a5efe80d695db7d7a7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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