What are some common methods for zipping folders in PHP?

To zip folders in PHP, you can use the ZipArchive class which provides methods to create, open, and extract zip archives. You can iterate through the files in the folder and add them to the zip archive using the addFile method. Finally, you can save the zip archive using the close method.

$zip = new ZipArchive();
$zipFileName = 'archive.zip';

if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
    $dir = 'folder_to_zip';
    $files = scandir($dir);
    
    foreach ($files as $file) {
        if ($file !== '.' && $file !== '..') {
            $zip->addFile($dir . '/' . $file, $file);
        }
    }
    
    $zip->close();
    echo 'Folder zipped successfully!';
} else {
    echo 'Failed to create zip archive!';
}