Tuesday, November 14, 2017
Create ZIP file with PHP
This is a script to create zip file containing different files. PHP has a ZipArchive class which can be used easily to create zip files.
In this post we will learn how to create zip files in PHP. We will create zip files from different files.
// defined an array with names of files
$files = array('audio1.wav', 'audio2.wav', 'audio3.wav', , 'img1.png', 'img2.jpg');
// the name of zip archive
$zipname = 'file.zip';
//create ner class for zip
$zip = new ZipArchive;
//create new zip
$zip->open($zipname, ZipArchive::CREATE);
//make a loop for adding files into zip file
foreach ($files as $file) {
$zip->addFile($file);
}
//close zip
$zip->close();
This script is about creating zip file in PHP in the same location (folder). If we have different files we must defined the full location of files.
$files = array('./folder1/audio1.wav', './folder1/audio2.wav', './folder7/audio3.wav', 'img1.png', 'img2.jpg');
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment