Create a Zip file using PHP

July 26, 2022  ‐ 1 min read

The archive extensions "Zip" allows you to create Zip archives with PHP. We can easily create Zip files by making use of the ZipArchive class.

<?php

$zip = new ZipArchive();

$zip->open('./destination.zip', ZipArchive::CREATE);

// addFile('file path', 'file name in zip archive');
$zip->addFile('./README.md', 'README.md');

$zip->close();

After "opening" the Zip archive we can start adding files, at last we need to call the close method to properly save the changes.