What are some alternative methods for creating .zip files in PHP without using shell_exec()?

Using shell_exec() to create .zip files in PHP can be a security risk as it allows for arbitrary shell commands to be executed. To avoid this risk, alternative methods for creating .zip files in PHP include using PHP's ZipArchive class or third-party libraries like PclZip.

// Using PHP's ZipArchive class to create a .zip file
$zip = new ZipArchive();
$zipFileName = 'example.zip';

if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
    $zip->addFile('file1.txt');
    $zip->addFile('file2.txt');
    $zip->close();
    echo 'Zip file created successfully.';
} else {
    echo 'Failed to create zip file.';
}