0

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

user3818090
  • 123
  • 8

2 Answers2

2

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,
));
lsouza
  • 2,448
  • 4
  • 26
  • 39
  • Just added `$zip->send_http_headers();` and the end of file before `$zip->finish()` and it seem the class not supported it ! – user3818090 Sep 12 '14 at 12:25
  • My bad. Just updated the answer after reading a bit more the lib :) – lsouza Sep 12 '14 at 12:27
  • This not resolved the problem :) he display the ZIP file only :) instead of download it ! – user3818090 Sep 12 '14 at 12:30
  • I added `header('Content-type: application/zip');` to test if problem come from the class, but same thing, display the zip too, answer must be on the server side (Configuration on Apache or Httpd) – user3818090 Sep 12 '14 at 12:34
  • Could you check if there's any content or notice/error being displayed before the zip content? If any content is echoed before the zip, it won't download. – lsouza Sep 12 '14 at 12:35
  • I checked and there no error ! i even put `error_reporting(0)` ! and i am using jquery to call the file for download ! when i browse the file directly he download and in the case where i call it from jquery w won't download ! – user3818090 Sep 12 '14 at 12:37
  • Oh, you forgot to mention this *little* detail :) open it in a _blank window instead, since the download will start automatically, your browser will close the window right away. Several websites use this way for downloading attachments. – lsouza Sep 12 '14 at 12:40
  • It's not easier than that :( i give a choice to a user to select products and then he click download to see them on a ZIP file ! and the website FULL AJAX :) i must done it with jquery too :( – user3818090 Sep 12 '14 at 12:42
  • I meant that you should not use AJAX for this specific request. You can open the url right when the user clicks on the button. It won't redirect him to any page, it just will start the download and the user will remain on the same page. No refreshs, nothing, just the download. – lsouza Sep 12 '14 at 12:55
  • Your problem **now** is not Apache, is JavaScript. If you don't want to link directly to the request, try this answer: http://stackoverflow.com/a/9970672/209585 – lsouza Sep 12 '14 at 12:57
  • Thank you for your help, but problem solved with this: https://github.com/filamentgroup/jQuery-File-Download – user3818090 Sep 12 '14 at 13:06
0

This should work:

$filename = "test.zip";
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($filename);
Tibor B.
  • 1,680
  • 1
  • 10
  • 12