简体   繁体   中英

Archive format is not recognised. ZipArchive in PHP not returning a valid format

Thanks in advance for your help

This is happening in a Ubuntu 20.10 machine and a server with apache and php 5.2

Let me paste the code:

$elementsToDownload = array(
    'productTableCSV' => $productTableCSVString,
    'productTableLangCSV' => $productTableLangCSVString,
    'productTableShopCSV' => $productTableShopCSVString
);

// var_dump($elementsToDownload);

$zipname = 'productCSVs.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($elementsToDownload as $key => $csvString) {
    if (! $zip->addFromString("$key.csv", $csvString)) {
        echo 'Could not add file to ZIP: ' . "$key.csv";
        die();
    }
}
$zip->close();

ob_clean();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));

readfile($zipname);
unlink($zipname);

It looks like something should be wrong in here, as I, randomly, can download the zip. From time to time the browser complains about not being able to download the file. Although this is not always happening, this is the message that Firefox display:

/tmp/mozilla_manu0/h03iaHVG.zip.part could not be saved, because the source file could not be read.

Try again later, or contact the server administrator.

This happens in Firefox.

Nor Mate Engrampa Archive Manager neither XArhiver are capable of opening the file. Surprisingly if I run unzip -t productCSVs.zip in the command line, it doesn't report any error. I can also unzip the file without the -t option if I do in the terminal, but no desktop app can open it.

To add some extra information, the PHP version which I'm using to generate the file is quite old, 5.2, but this is what I have until my company updates their systems, which is expected in the future.

Can anybody think of any reason why this could be happening?

EDIT:

I just realised that even though I don't have errors when decompressing the file in the terminal, I have a warning about 1 extra byte at the beginning or within zipfile:

manu@manu-NUC8i7BEH:/tmp/mozilla_manu0$ unzip -t productCSVs.zip 
Archive:  productCSVs.zip
warning [productCSVs.zip]:  1 extra byte at beginning or within zipfile
  (attempting to process anyway)
    testing: productTableCSVString.csv   OK
    testing: productTableLangCSVString.csv   OK
    testing: productTableShopCSVString.csv   OK
    testing: productTableCSV.csv      OK
    testing: productTableLangCSV.csv   OK
    testing: productTableShopCSV.csv   OK
No errors detected in compressed data of productCSVs.zip.

I still can't understand what's going on in here

Ok, so I solved the issue. My problem was that I had opened and closed two times the php tags in my code, leaving a space in between. Something like this:

?>

<?php

As a result that space was being added to the download stream.

I removed the closing tag and the opening tag, as they were together, and now every file I download from the server is recognised by all my zip tools

Thank you for having read me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM