Search code examples
cakephpcakephp-2.4

Can I force download mutiple files or a zip file?


I'm using Cakephp 2.4.

I can actually force download a file (created from string) at each request with the code below:

$this->autoRender = false;
$this->response->body($file);
$this->response->download($log);
$this->response->send();

But it's possible to download multiple files in one request? When I put this code in a loop, Cake create one file with the content of the differents files in.

If it's not, is it possible with Cake to create a zip file and put each file in?


Solution

  • That's not possible, simply because neither the HTTP protocol nor any browser do support something like that, it's not a CakePHP thing.

    If you need to serve multiple files with a single request then you'll have to put them in some kind of archive, however that also isn't directly related to CakePHP as it doesn't ship with a ZIP library or similar, it's all about PHP.

    I don't think there's any need at this point to explain how to ZIP files with PHP, as this is something that is explained on every corner of the internet.

    Once you've zipped your files, either pass the zipped data to CakeResponse::body() as a string like you're already doing (not recommended for large archives as it will blow up the memory usage), or temporarily store the ZIP file on disk and use CakeResponse::file().

    http://book.cakephp.org/2.0/en/controllers/request-response.html#sending-files