Skip to content

Instantly share code, notes, and snippets.

@michalkortas
Created January 14, 2019 08:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalkortas/6fd2af781ec1708fd30faf3c5b56f0fa to your computer and use it in GitHub Desktop.
Save michalkortas/6fd2af781ec1708fd30faf3c5b56f0fa to your computer and use it in GitHub Desktop.
<?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