my download zip code not working. The zip contains of some xls files
I got error when attempting to generate export file.
I’m using PHP.
The code is like this:
$directory = Yii::getAlias('@console/runtime/tmp');
if (!file_exists($directory)) {
mkdir($directory);
}
$this->zip = new ZipArchive();
$this->zipFile = tempnam($directory, "zip");
....
$html = '<table><thead><tr><th>no</th><th>name</th></tr></thead><tbody></tbody></table>';
$output = '';
$output .= "<html>n";
$output .= "<head>n";
$output .= "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />n";
$output .= "</head>n";
$output .= "<body>n";
$output .= $html;
$output .= "</body>n";
$output .= "</html>n";
$fileName = "test" . '.xls';
if ($this->zip->open($this->zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
file_put_contents($fileName, $output);
$this->zip->addFile($fileName, basename($fileName));
}
$this->zip->close();
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=' . $this->getFilename());
header("Content-Length: " . filesize($this->zipFile));
header("Pragma: no-cache");
header("Expires: 0");
readfile($this->zipFile);
exit();
The result
what should I do to make it work? I already spent 3 days on this problem