John Davidson

How to force download zip file in php (zend framework)

0 comments
Message:


I'm triying to download a zip file using the following code


$archive_file_name ='facturas.zip';
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".$archive_file_name);
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
readfile($archive_file_name);

But this return the following, and the download don't start.



My Answer:
"Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".$archive_file_name);
readfile($archive_file_name);

However, this code may not work in Zend Framework as it has its own way of handling file downloads. Here is an example of how you can force download a zip file in Zend Framework:

php
public function downloadAction()
{
$archive_file_name = 'facturas.zip';
$this->getResponse()
->setHeader('Content-Type', 'application/zip', true)
->setHeader('Content-Disposition', 'attachment; filename=' . $archive_file_name, true)
->setHeader('Content-Length', filesize($archive_file_name), true)
->sendHeaders();

readfile($archive_file_name);
exit;
}


In this code snippet, we are setting the headers using the `setHeader` method of the Zend Framework `getResponse` object. We are also sending the file using the `readfile` function and then exiting the script to prevent any further output.

Make sure to replace `facturas.zip` with the actual path to your zip file.

Rate this post

4 of 5 based on 3849 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us