How can PHP handle creating multiple files within a ZIP archive?
To create multiple files within a ZIP archive using PHP, you can use the ZipArchive class provided by PHP. You can open a new ZIP archive, add files to it using the addFile() method, and then close the archive to save the changes. This allows you to easily create a ZIP archive containing multiple files.
$zip = new ZipArchive();
$zipFileName = 'archive.zip';
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
$file1 = 'file1.txt';
$file2 = 'file2.txt';
$zip->addFile($file1);
$zip->addFile($file2);
$zip->close();
echo 'ZIP archive created successfully.';
} else {
echo 'Failed to create ZIP archive.';
}
Keywords
Related Questions
- How can the issue of users deleting cookies after browsing impact the functionality of auto login features in PHP?
- What are the common pitfalls to avoid when transferring ownership of a PHP project and its associated database?
- What security considerations should be taken into account when allowing users to input and display content on a website using PHP?