Popular Posts

Thursday, 10 April 2014

How to create a zip file using php and delete it after user downloads it


File Name :- download.php

<?php

$valid_files = array();if(is_array($files)) {
    foreach($files as $file) {
        if(file_exists($file)) {
            $valid_files[] = $file;
        }
    }
}

if(count($valid_files > 0)){
    $zip = new ZipArchive();
    $zip_name = "fb.zip";
    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
        $error .= "* Sorry ZIP creation failed at this time";
    }
    
    foreach($valid_files as $file){
        $zip->addFile($file);
    }
    
    $zip->close();
    if(file_exists($zip_name)){
        // force to download the zip
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
        readfile($zip_name);


        // remove zip file from temp path
        //unlink($zip_name);
    }

} else {
    echo "No valid files to zip";
    exit;
}



?>

Enjoy ...............................................:)

Note : - Root Directory upload Zip folder.

http:// yourdomain.com/download.php

No comments:

Post a Comment

Magento: How to get last order id

There are many ways to get last order id:   1. From checkout session: $lastOrderId = Mage::getSingleton('checkout/session'...