$zip = new ZipArchive(); $zip->open('file.zip', ZipArchive::CREATE); $fileContents = "This is a sample file."; $zip->addFromString('sample-file.txt', $fileContents); $zip->close();
$zip = new ZipArchive(); $zip->open('file.zip', ZipArchive::CREATE); $file1 = "This is file 1."; $file2 = "This is file 2."; $zip->addFromString('file1.txt', $file1); $zip->addFromString('file2.txt', $file2); $zip->close();This example code creates a new ZIP archive called 'file.zip', then adds two files 'file1.txt' and 'file2.txt' with contents 'This is file 1.' and 'This is file 2.' respectively using addFromString(). Package library: The ZipArchive class is part of the PHP's core zip extension, which needs to be enabled in the PHP configuration file (php.ini) to use this functionality.