What are some PHP functions or libraries that can be used to create zip files?
To create zip files in PHP, you can use the built-in ZipArchive class. This class provides methods for creating, adding files to, and closing zip archives. By using this class, you can easily create zip files in your PHP application.
$zip = new ZipArchive();
$zipFileName = 'example.zip';
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
$zip->addFile('file1.txt', 'file1.txt');
$zip->addFile('file2.txt', 'file2.txt');
$zip->close();
echo 'Zip file created successfully.';
} else {
echo 'Failed to create zip file.';
}
Related Questions
- How can PHP scripts be structured to validate form input fields, including email addresses, efficiently?
- How can a directory be listed on a website with images and PDF files using CSS styling in PHP?
- How can you ensure that only key-value pairs with both a key and a value are output from an array in PHP?