1
I am trying to compress two files together in PHP, but I get the following error while trying to open . zip:
! C:\Users\gpsilva\Downloads\your_name (19).zip: Final inesperado do arquivo
This problem only happens when I add the second file. When there is only one, the problem does not happen and I can open the . zip normally.
$spreadsheet->setActiveSheetIndex(0);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
$excel_file_tmp = tempnam("/tmp", 'DGO-Acesso');
$writer->save($excel_file_tmp);
//zip
$zip_file_tmp = tempnam("/tmp", 'DGO');
$zip = new ZipArchive();
$zip->open($zip_file_tmp, ZipArchive::OVERWRITE);
$zip->addFile($excel_file_tmp, 'DGO-Acesso.xls');
$zip->close();
//download
$download_filename = 'DGO-Download.zip';
header("Content-Type: application/zip");
header("Content-Length: " . filesize($zip_file_tmp));
header("Content-Disposition: attachment; filename=\"" . $download_filename . "\"");
readfile($zip_file_tmp);
unlink($excel_file_tmp);
unlink($zip_file_tmp);
die;
If I double this line:
$zip->addFile($excel_file_tmp, 'DGO-Acesso2.xls');
Changing only the file name, to come both zipped, the error already starts to happen.
@felipecsweb The error persists if I take one of the add line it comes back working, but thanks for the improvement tips! I will continue hunting error :(
– Guilherme Prado
The problem was in "header("Content-Length: ". filesize($zip_file_tmp));" for some reason the header size was getting smaller than the actual size of the zipped file and so the download was done missing "pieces", the solution was just to comment on this line.
– Guilherme Prado