Search code examples
phpapacheubuntuzipstream

Apache read ZIP files instead of downloading


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 !!


Solution

  • 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,
    ));