Question

I'm editing an ancient script that zips several images and presents them dynamically to the user.

I have rewritten almost all of the code, but I can't find a way to way to output the contents of the zipfile. Writing it to the server is very undesirable.

I create the file with:

$z = new ZipArchive();

I can add content with:

$z->addFromString("filename",$string);

And I want to present it dynamically with:

header("Content-Type: application/zip;");
header("Content-Disposition: attachment; filename=file.zip;");

// I need a function to read the contents of the zipfile here. Something like:
echo $z->filecontent();

I can't find out what function to use for this.

Was it helpful?

Solution

You would open the file, creating it most likely with temp name. Something like this:

$name = tempnam('/tmp','zip');
$z->open($name, ZIPARCHIVE::CREATE)

After you finish adding all your files, you would close it.

$z->close();

Now when you are ready to send the data you would do this:

readfile($name);

After you are done, you want to clean up the temp file with:

unlink($name);

OTHER TIPS

If you read the documentation, look at the close() method to actually save the file physically to the filesystem. Then you can use readfile() on the saved file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top