0

我的脚本有问题,当我在创建后尝试下载 ZIP 文件时 - apache 读取它们而不是下载!

我使用 zipstream.php 类(https://github.com/maennchen/ZipStream-PHP

如何配置 apache(在 Ubuntu 上运行)以允许下载带有 ZIP 扩展名的文件?

谢谢 !

我正在使用的代码:

<?php
if($_GET['download'] == "ok")
{

$id = $_GET['id'];

$content = "TEST";

$mysql = $db_1->query("select result from testing where id='$id'");
$row = mysql_fetch_assoc($mysql);
$content .= $row['result'];

$file_opt = array(
  'time'    => time() - 2 * 3600,
  'comment' => 'Simple Comment !',
);

$zip = new ZipStream('test.zip', array(
  'comment' => 'Simple Comment !'
));

$zip->add_file('test.txt', $content, $file_opt);
$zip->finish();

exit;

}

注意:问题是当我从 JQUERY 调用文件时他不会下载,但是当我直接浏览它时他会正确下载!

4

2 回答 2

2

您可能忘记在回显内容之前设置 zip 标头。在打印 zip 内容之前尝试此操作:

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="myFileName.zip"');

更新:您的库似乎有一个正确的方法来发送 zip 标头。尝试改用这个:

$zip = new ZipStream('test.zip', array(
    'comment' => 'Simple Comment !',
    'send_http_headers' => true,
));
于 2014-09-12T12:19:14.067 回答
0

这应该有效:

$filename = "test.zip";
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($filename);
于 2014-09-12T12:24:03.000 回答