tags:

views:

201

answers:

2

Hi!

In a single script, I need to do the following:

  1. create a zip out of files in a directory
  2. force the download of the newly create zip

My problem is that whenever I try to do this in a single script, the downloaded zip is corrupted (filesize is ok though). If I trig the processes in two separate script calls, the downloaded zip is ok.

I guess that the problem is that the zip saving to file process isn't completely finished before the download starts. Strangely, it doesn't solve the problem to insert a sleep(3) between the processes... Code outline below.

How to assure that the zip file is completely finished before the force download starts?

Regards / Jonas

// 1. create a zip
$createZipFile = new CreateZipFile('temp.zip');
$createZipFile->zipDirectory('temp/', '.');
$createZipFile->saveZipFile();

sleep(3); // <-- Doesn't matter!

// 2. force zip download
$fileServer = new FileServer();
// Line below gives a corrupted zip when run in same script as 1.     
$fileServer->forzeDownload('temp.zip');
+1  A: 

Create the zip and then redirect the user to that file.

http://php.net/manual/en/function.header.php

AntonioCS
Thanks Antonio, but in this case I have to do it in a single process...
Cambiata
You are doing it in a single process. You already create the file, just either use the header to pass the appropriate headers to download a file and then use readfile. Or simply redirect the user to the zip file (which is a lot simpler)
AntonioCS
IF you do that, then don't forget to implement some kind of solution that deletes files that have already been served.
Anti Veeranna
A: 

Thank you, JorenB and Gumbo! The texteditor examination revealed some debug output in zip creation that doesn't affect the orignial zip, but corrupts the data sent to the browser when downloading.

Cambiata