Apache read ZIP files instead of downloading

1.3k Views Asked by At

I have problem with my script, when i try to download ZIP files after creating - apache read them instead of downloading !!!

I use zipstream.php class (https://github.com/maennchen/ZipStream-PHP)

How to configure apache (running on Ubuntu) to let downloading this files with ZIP extension ?

Thank you !

Code i am using:

<?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;

}

Note: The problem is when i call the file from JQUERY he won't download, but when i browse it directly he download correctly !!

2

There are 2 best solutions below

11
lsouza On BEST ANSWER

You're probably forgetting to set the zip header before echoing the content. Try this before you print the zip content:

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

Update: your lib seem to have a proper method to send the zip headers. Try to use this instead:

$zip = new ZipStream('test.zip', array(
    'comment' => 'Simple Comment !',
    'send_http_headers' => true,
));
1
Tibor B. On

This should work:

$filename = "test.zip";
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($filename);