$zip = new ZipArchive(); $filename = "test.zip"; if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) { exit("cannot open <$filename>\n"); } $file_path = "path/to/myfile.txt"; $zip->addFile($file_path, "myfile.txt"); $zip->close();
use PhpZip\ZipFile; $zip = new ZipFile(); $file_path = "path/to/myfile.txt"; $zip->addFile($file_path, "myfile.txt"); $zip->outputAsAttachment("test.zip");This example uses a package library called PhpZip. The addFile() method is used to add the file myfile.txt to a Zip archive. The outputAsAttachment() method is used to send the Zip archive as an attachment to the user.