Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

Create and download Zip folder in php
ini_set("memory_limit","1280000M");
ini_set('max_execution_time', 60000);

// Zip destination path
$zipDestination =  __DIR__.'/images/zipfile.zip';
// Use php zip library
$zip = new ZipArchive();
touch($zipDestination); // create zip files
if($zip->open($zipDestination, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$zipDestination>\n");
}
// If you want to add all files from folder 
$pattern = __DIR__.'/bagImages';
// It will read all files of bagImages folder
$files = glob($pattern.'/*.*');

foreach($files as $file) {
$fileinfo = pathinfo($file);
$zip->addFile($file,$img_fol.'/'.$fileinfo['basename']);
}

// if you want add sub folder files also use.
$rootPath = realpath('images');
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}
$zip->close();
$filename = "zipfile.zip";
$filepath = __DIR__.'/productimages/';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_clean();
flush();
set_time_limit(0);
@readfile($filepath.$filename);

@unlink($filepath.$filename); // if you want to delete files.

No comments:

Post a Comment

Bottom Ad [Post Page]