I am using PCLZIP to create a ZIP that contains few documents and pdfs. On some machine, users are claiming zip to be corrupted.
On my machines, I don't see ZIP to be corrupted, but whenever I try to send that zip via Gmail, it blocks the ZIP saying it contains an executable, but in fact all that zip have are documents and PDF files. So I see relation between people claiming that zip are corrupted and Gmail blocking these zip created from our app.
I have tried extracting all the files and again adding them in a zip using Window's add to archive option. And if I send this zip, gmail doesn't block.
So I believe there has to be some problem when I am creating ZIP in my PHP app.
Here is the relevant portion of the code where I am creating ZIP:
<?php
$archive = new PclZip("exportZIPs/{$randomFileName}.zip");
$orgFile = $document->sAbsStoragePath.$document->sSysFileName;
$filePath = array();
$filePath[] = array( PCLZIP_ATT_FILE_NAME => $orgFile,
PCLZIP_ATT_FILE_NEW_FULL_NAME => "{$someNewName}"
);
foreach ($aList as $key => $value) {
$filePathTmp = "{$SomePath}.pdf";
if(!file_exists($filePathTmp)) {
continue;
}
//$filePath[] = "{$SomePath}.pdf";
$filePath[] = array( PCLZIP_ATT_FILE_NAME => $filePathTmp,
PCLZIP_ATT_FILE_NEW_FULL_NAME => "PDFs/Version-{$sVersion}.pdf"
);
}
$v_list = $archive->add($filePath, PCLZIP_OPT_REMOVE_PATH, 'PDFs');
if ($v_list == 0) {
die("Cannot Create Archive! Try again or contact administrator");
die("Error : ".$archive->errorInfo(true));
}
header("Content-Type: application/zip");
header("Content-Disposition:attachment;filename=\"{$SOMENAME}.zip\"");
readfile("exportZIPs/{$randomFileName}.zip");
@unlink("exportZIPs/{$randomFileName}.zip");
?>
What am I doing wrong?
Finally I found out, the problem wasn't with creating archive. Problem was while sending the content data.
I added following lines to code and it fixed it:
header("Content-Type: application/octet-stream");
header("Content-Disposition:attachment;filename=\"{$document->iID}-{$document->sName}.zip\"");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$originalFile = "exportZIPs/{$randomFileName}.zip";
header('Content-Length: ' . filesize($originalFile));
ob_clean();
flush();
readfile("exportZIPs/{$randomFileName}.zip");
@unlink("exportZIPs/{$randomFileName}.zip");
The reason it too so long to identify this was that winZIP and other ZIP app were autocorrecting problem and wasn't reporting archive to be corrupted.