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