What are common issues when zipping files with PHP and how can they be avoided?

One common issue when zipping files with PHP is running out of memory when trying to compress large files or a large number of files. This can be avoided by increasing the memory limit in the PHP configuration or by chunking the files into smaller parts before zipping them.

// Increase memory limit
ini_set('memory_limit', '256M');

// Chunk files before zipping
$files = array('file1.txt', 'file2.txt', 'file3.txt');
$zip = new ZipArchive();
$zip->open('archive.zip', ZipArchive::CREATE);

foreach ($files as $file) {
    $zip->addFile($file);
}

$zip->close();