2

Currently wondering if anyone has experienced corrupt zip files. I'm currently using the following code

// Multiple File Downloads

require_once('includes/pclzip.lib.php');

$archive = new PclZip('archive.zip');

$v_list = $archive->create('robots.txt,index.php');

if ($v_list == 0) {
  die("Error : ".$archive->errorInfo(true));
}

$file_url = 'archive.zip';

header('Content-Type: application/zip');

header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 

readfile($file_url);

To create a zip archive and force the zip download, eventually this will be initiated by a button click but for now I'm simply trying to create an archive that actually works.

I'm getting the issue regardless of whether or not I use the built in ZipArchive class or use the PclZip library.

One thing that's very odd is that the zip that's created and placed on the server works just fine when I downloaded it via FileZilla but all bets are off once if I force the file downloaded. I've tried in multiple browsers and the results are the same for each.

Really annoying. I'll admit that this is my first time attempting to implement this type of functionality so perhaps I'm missing something. I've seen a few threads on similar issues but unfortunately the solutions are not working for me. Any help that can be provided would be much appreciated.

Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
jasenmp
  • 319
  • 6
  • 17
  • You don't close the `zip` file, you don't save it... Also, check to see if your server is using GZip compression to send the content. And theck that you **don't close** the PHP code (aka, dont use `?>` at the end). – Ismael Miguel Mar 23 '15 at 18:55

1 Answers1

5

If it works on server and is corrupted only when downloaded - most probably PHP is echoing something (notice? warning?) along with ZIP content, so it is broken when it's downloaded. Try to open your downloaded ZIP with text editor and check for some PHP output in the begginning or at the end of it. On linux it would be:

cat downloaded.zip | tail
cat downloaded.zip | head
Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
  • @jasenmp - How's it going? Did you managed to solve your problem? – Paweł Tomkiel Mar 30 '15 at 06:59
  • Hi Paul, my apologies for abandoning this thread. I'm up against a deadline. I just tried to open it in notepad but I'm not seeing anything that's PHP related. Do you have any recommended windows text editors I can use that might display the PHP output? – jasenmp Mar 30 '15 at 20:55
  • If i'm right - you should see it right away on the top or bottom of file in any text editor (even Notepad). – Paweł Tomkiel Mar 31 '15 at 06:42
  • Ah I see. I just double checked to make sure I wasn't overlooking anything. It's not a huge issue at the moment, but I'd like to figure it out. It will bug me. I'll continue to tinker with it as often as possible and look for a solution. – jasenmp Mar 31 '15 at 13:14