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!';
}
Keywords
Related Questions
- Are there best practices or frameworks in PHP for implementing real-time features like chat rooms, considering the limitations of traditional request-response architecture?
- How can I extract the date and time into a variable in PHP to insert into a DATETIME format field in a database?
- What are some best practices for optimizing code efficiency when working with arrays in PHP?