Tuesday, November 19, 2013

how to create zip file to multiple pdf, image, and other documents using php

Step-1> simple create a zipcreation.php and paste below those code and run some change are destination url and your multiple file location have to change
----------------------------------------zipcreation.php---------------------------------------------------
<?php
//Author : Bikash ranjan nayak
/* creates a compressed zip file */
function Create_YourFilestoZip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//class ZipArchive is php built libary class
$zipobj = new ZipArchive();
if($zipobj->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zipobj->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

//close the zip -- done!
$zipobj->close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}

$list_of_files_for_zip = array(
'D:\xampp\htdocs\test/bikash-ranjan-nayak.pdf',
'D:\xampp\htdocs\test/bikash-ranjan-nayak.gif'


);
//if true, good; if false, zip creation failed
if(Create_YourFilestoZip($list_of_files_for_zip,'D:\xampp\htdocs\test\createpdf/bikash-ranjan-nayak.zip'))
{
    echo "zip file has been created success fully";
 }else
 {
    echo "zip file creation fail";
 }
-------------------------------------------------------------------------------------------------

No comments:

Post a Comment