Created
January 14, 2019 08:48
-
-
Save michalkortas/6fd2af781ec1708fd30faf3c5b56f0fa 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 | |
namespace App\Services; | |
use ZipArchive; | |
class ZipArchiveService | |
{ | |
public function makeZip() | |
{ | |
$downloadDir= '../download'; | |
$archiveFileName = 'archive.zip'; | |
// create zip archive | |
$zip = new ZipArchive; | |
if ($zip->open($downloadDir. '/' . $archiveFileName, ZipArchive::CREATE) === TRUE) | |
{ | |
// add file/s | |
$file = '../path/to/file.pdf'; | |
if(file_exists($file)) | |
$zip->addFile($file, 'New Name.pdf'); | |
} | |
// download file | |
$headers = array( | |
'Content-Type' => 'application/octet-stream', | |
); | |
if(file_exists($downloadDir. '/' . $archiveFileName)) | |
{ | |
return response()->download($downloadDir. '/' . $archiveFileName, $archiveFileName, $headers); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment