As a Web Developer, It is very necessary to have a knowledge about how to extract Zip Files using PHP. In this blog, I am going to create a Zip file with using PHP Code. To execute this task, I will use a built-in extension in PHP known as ZipArchive class.
This is a very basic PHP function that accepts two parameters array of files to be zipped and name of a zip file to be created.
//Creating a Zip File Using PHP
function genZip($files = array(),$zipName){
$zip = new ZipArchive();
$zip->open($zipName.'.zip', ZipArchive::CREATE);
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
}
Usage :-
//Usage of genZip function
$files = array(
'file1.pdf',
'file2.pdf',
'file3.pdf',
'folder2/file4.pdf',
'folder2/file5.pdf'
);
$zipName = 'myfiles';
genZip($files,$zipName);
This is a very basic PHP function that accepts two parameters array of files to be zipped and name of a zip file to be created.
//Creating a Zip File Using PHP
function genZip($files = array(),$zipName){
$zip = new ZipArchive();
$zip->open($zipName.'.zip', ZipArchive::CREATE);
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
}
Usage :-
//Usage of genZip function
$files = array(
'file1.pdf',
'file2.pdf',
'file3.pdf',
'folder2/file4.pdf',
'folder2/file5.pdf'
);
$zipName = 'myfiles';
genZip($files,$zipName);
0 comments:
Post a Comment