Home > Software engineering >  Download PHP ZipArchive returns corrupt, blank zip file
Download PHP ZipArchive returns corrupt, blank zip file

Time:07-26

I've had a look at various threads about ZipArchive and how to get files to download. However, I've gotten a bit stuck here. I currently have a form that looks like this

<form name="download-resources" method="post">
    <input type="hidden" name="download-resources" value="1" />
    <?php foreach($resources as $key => $value) { ?>
        <label for="<?php echo $value->name; ?>"><?php echo $value->name; ?> 
        <input type="checkbox" name="download-items[<?php echo $key; ?>]" download-id="<?php echo $key; ?>" /></label>
    <?php } ?>
    <input type="submit" value="Download"/>
</form>

Within that form, are some aspects that come from the rest of the script, which is here:

<?php

$resources = array();

// Create our items
$i = 1;
while($i <= 3) {
    ${'item-' . $i} = new stdClass();
    ${'item-' . $i}->name = 'item-' . $i;
    ${'item-' . $i}->link = 'link';
    $resources[$i] = ${'item-' . $i};
    $i  ;
}

// When we post the form
if(!empty($_POST['download-resources'])) {
    $archive_file_name = 'some resources.zip';
    $zip = new ZipArchive();
    if ($zip->open($archive_file_name, ZipArchive::CREATE) === TRUE) {
        foreach($_POST['download-items'] as $key => $value) {
            $zip->addFile('https://www.w3.org/TR/PNG/iso_8859-1.txt', 'test.txt');
            //$zip->addFile(${'item-' . $key}->link . '.txt');
        }
        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name");
    header("Content-length: " . filesize($archive_file_name));
    readfile("$archive_file_name");
    exit;
}

?>

I've tested all of the other bits, like the generation of the objects and such. But currently I'm just puzzled at why the files won't download properly. Whenever I hit the submit button to download, after selecting one or two of the items, I get given a zip file with the correct zip name - all fine and dandy. But when attempting to open the file using WinRaR, the following message appears:

The archive is either in unknown format or damaged

I have attempted the old trick of opening the zip file in notepad to try and see whether there are any errors present. However, the result that returns is completely blank, which is really weird to me.

Does anyone know how I might fix this? Thanks in advance

CodePudding user response:

Solved: Needed to create a temp folder, and store them there. Here's the final solution:

<?php if(!empty($_POST['download-resources'])) {
    $archive_file_name = "something resources.zip";
    $rand = rand(0,999999);
    $tmp_file = 'files/tmp/'.$rand.'.zip';
    $zip = new ZipArchive;
    if ($zip->open($tmp_file, ZipArchive::CREATE) === TRUE) {
        foreach($_POST['download-items'] as $key => $value) {
            
            $zip->addFile($_SERVER['DOCUMENT_ROOT'] . '/technology/tony/files/test1.txt', 'test1.txt');
            //$zip->addFile(${'item-' . $key}->link . '.txt');

        }
        $zip->close();
        header("Content-type: application/zip"); 
        header("Content-Disposition: attachment; filename=" . $archive_file_name . "");
        header("Content-length: ".filesize($archive_file_name));
        readfile($tmp_file);
    } 
    exit; } ?>

CodePudding user response:

This code will works for you.

// When we post the form
if (!empty($_POST['download-resources'])) {
    $archive_file_name = 'some resources.zip';
    $zip = new ZipArchive();
    if ($zip->open($archive_file_name, ZipArchive::CREATE) === TRUE) {
        foreach($_POST['download-items'] as $key => $value) {
            $file_url = 'https://www.w3.org/TR/PNG/iso_8859-1.txt';
            $download_file = file_get_contents($file_url);
            $zip->addFromString(basename($file_url),$download_file);
            //$zip->addFile(${'item-' . $key}->link . '.txt');
        }
        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }

    header("Content-type: application/zip");
    header("Content-Disposition: attachment; filename=$archive_file_name");
    header("Content-length: " . filesize($archive_file_name));
    readfile("$archive_file_name");
    exit;
}
  • Related