How can I prevent a ZIP archive from being automatically offered for download when using the ZIPlib class in PHP?
When using the ZIPlib class in PHP to create a ZIP archive, the archive may be automatically offered for download by the browser. To prevent this behavior, you can save the ZIP archive to a file on the server instead of outputting it directly to the browser. Then, you can provide a link for the user to download the file.
<?php
// Create a ZIP archive using ZIPlib
$zip = new ZipArchive();
$zipFileName = 'example.zip';
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
$zip->addFile('file1.txt');
$zip->addFile('file2.txt');
$zip->close();
}
// Provide a download link for the user
echo '<a href="' . $zipFileName . '">Download ZIP archive</a>';
?>
Keywords
Related Questions
- How can developers ensure that variable values stored in databases or files are securely accessed and updated in PHP applications?
- What is the potential issue with escaping characters in SQL queries when using PHP?
- What are the benefits and drawbacks of using absolute paths in Smarty templates for PHP projects?