I'm using ZipArchive to zip and download the dynamically generated pdf files. The files are getting generated in the designated folder but when I'm trying to download the zip file, the 1KB broken file is getting download.
I've tried this code at my local wampp and here it works perfectly and the proper zip file is getting generated but in production the broken zip is getting downloaded. I believe there's some server variable which might be resulting this?
// creating directory in the server with timestamp
$dirName = strtotime(date('Y-m-d H:i:s'));
$dirPath = "/var/www/...../foldertobezipped/" . $dirName;
mkdir($dirPath);
// generating and storing the generated pdf files in the $dirPath folder
foreach ($resultArray as $filelist) {
$this->invoicepdfview($orderid);
}
// zip file name
$archive = $dirName . '.zip';
// invoking ZipArchive
$zip = new ZipArchive;
$zipPath = $dirPath . "/" . $archive;
$zip->open($zipPath, ZipArchive::CREATE);
// scanning all the files to be zipped
$files = scandir($dirPath);
// removing the unwanted key-value from the array
unset($files[0], $files[1]);
// adding files to the zip file
foreach ($files as $file) {
$new_filename = substr($dirPath . '/' . $file, strrpos($dirPath . '/' . $file, '/') + 1);
$zip->addFile($dirPath . '/' . $file, $new_filename);
}
$zip->close();
// downloading the zipped file
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipPath);
header('Content-Length: ' . filesize($zipPath));
readfile($zipPath);
unlink($zipPath);
I've checked and compared most of the php.ini variables which could affect this between local WAMPP and production server. Could someone help me out of this? Where am I missing something?
P.S. - I've provided 777 permission to the directory where the files are being created.