What are common errors encountered when trying to zip multiple files in PHP?
Common errors when trying to zip multiple files in PHP include not specifying the correct file paths, not setting the correct permissions for the destination zip file, and not properly handling file permissions or ownership. To solve these issues, ensure that the file paths are correct, set appropriate permissions for the destination zip file, and handle file permissions properly during the zipping process.
$zip = new ZipArchive();
$zipFileName = 'archive.zip';
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
$files = ['file1.txt', 'file2.txt', 'file3.txt'];
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
echo 'Files zipped successfully.';
} else {
echo 'Failed to create zip file.';
}