Skip to content
PHP Logo

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.

php
$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.';
}