Php: How to handle file processing and uploading operations in one script?

In one script, I need to do the following:

  • create zip from files in directory
  • force download the newly created zip

My problem is that whenever I try to do this in one script, the downloaded zip gets corrupted (file size is ok anyway). If I processes in two separate calls to the script, the downloaded zip is fine.

My guess is that the problem is that saving the zip file to a file is not complete before starting the download. Oddly enough, this does not solve the problem insert sleep (3) between processes ... Code diagram below.

How to assure that the zip file is completely complete before 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'); 

      

+2


source to share


2 answers


Thank you JorenB and Gumbo! The textualizer exam revealed some debug output in the zip generation that does not affect the original zip code, but corrupts the data sent to the browser on load.



0


source


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



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

+1


source







All Articles