Download two Zipped files in PHP

Asked

Viewed 80 times

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.

1 answer

1


Your code seems to be correct, but probably the error has to do with the fact that you are not specifying the file name format.

See your reworked code:

$spreadsheet->setActiveSheetIndex(0);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
$excel_file_tmp = tempnam("/tmp", 'DGO-Acesso');
$writer->save($excel_file_tmp);

//zip
$current_dir = dirname( __FILE__ );
//$zip_file_tmp = tempnam("/tmp", 'DGO');
$zip_file_tmp = uniqid( 'DGO' );
$zip_file_tmp .= '.zip';
$zip        = new ZipArchive();
//$zip->open($zip_file_tmp, ZipArchive::OVERWRITE);
$zip->open($zip_file_tmp, ZipArchive::CREATE);
$zip->addFile($excel_file_tmp, 'DGO-Acesso.xls');
$zip->addFile($excel_file_tmp, 'DGO-Acesso2.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;

Some points:

  1. When you use the function tempnam() it automatically creates a file, in which case it is more interesting to use something like the function uniqid() that you returns a single string.
  2. And since the file name will always be unique, it is necessary to use ZipArchive::CREATE.

I tested it here and it all worked out.

Run the tests on your machine.

  • @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 :(

  • 1

    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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.