php

How to create Zip folder and download in Cake PHP

Spread the love

How to create Zip folder and download in Cake PHP –

Step : 1 – First create Hyperlink for download zip folder in studentdetail.ctp file and add bellow codes –

<p>
<!-- student id = 10, you can also pass dynamic student id value from here -->
<a href="<?php echo $this->webroot; ?>Student/downloadzipdata/10"><b>Download All Documents (Zip File)</b></a>
</p> 
  • Student” is Controller name and
  • downloadzipdata” is function/action name of StudentController.php

Step : 2 – Create RegistrationDetails.php Model and bellow codes –

<?php
class RegistrationDetails extends AppModel {    
    public $name = "RegistrationDetails";
    public $useTable = "student_registration_detail"; //MySQL Table Name    
}
?>

Step : 3 – Create StudentController.php and add bellow codes –

<?php 
App::uses('AppController', 'Controller');
class StudentController extends AppController {
    public $uses = array('RegistrationDetails');
    var $helpers = array('Html', 'Form');

public function downloadzipdata() {	
  $studentId = $this->request->params['pass'][0];  // get student id from created Hyperlink in studentdetail.ctp file
  $StudDetails = $this->RegistrationDetails->find('first', array('conditions' => array('RegistrationDetails.id' => $studentId)));
	$fname = $StudDetails['RegistrationDetails']['name'];
  
  // define zip folder name with student name & id
	$zipFolderName = $fname."_".$StudDetails['RegistrationDetails']['id'];
        
	$filerecords = array();
	$files_to_zip_path = array();
	$datas = array(		
		// get files name from database table
		$StudDetails['RegistrationDetails']['id_proof_file'],                                          
		$StudDetails['RegistrationDetails']['caste_certificate_file'],                     
		$StudDetails['RegistrationDetails']['high_school_marksheet_file'],
		$StudDetails['RegistrationDetails']['ug_marksheet_file'],                     
		$StudDetails['RegistrationDetails']['pg_marksheet_file'],                                         
		$StudDetails['RegistrationDetails']['work_experience_file'],                                         
	);  

	foreach ($datas as $files_to_zip) {
		if (!empty($files_to_zip)) {				
			// provide full file path where files stored
			$files_to_zip_path[] = "/studentsdata/app/webroot/" . $files_to_zip;
			$temp = explode('/', $files_to_zip);
			$filerecords[] = end($temp);
		}
	}
	// call function with parameters 
	$result = $this->create_zip($files_to_zip_path, 'uploads/my-archive.zip', false, $filerecords,$zipFolderName);  
	exit;
}
   
// define create_zip function 
function create_zip($files = array(), $destination = '', $overwrite = false, $filerecords, $zipFolderName) {
	// create new zip object
	$zip = new ZipArchive();

	// create a temp file & open it
	$tmp_file = tempnam('.', '');
	$zip->open($tmp_file, ZipArchive::CREATE);
	$newFile = '';
	$k = 0;
	// loop through each file
	foreach ($files as $file) {
		// download file
		if ($newFile == $filerecords[$k]) {
			$rr = $file;
			$parts = explode("/", $file);
			$fileHand = fopen($file, 'r');
			fclose($fileHand);
		 
			$rrr = rename($rr, $file);
			$download_file = file_get_contents($file);
			
			//add it to the zip
			$zip->addFromString(basename($file), $download_file);
		} else {
			$download_file = file_get_contents($file);
			//add it to the zip
			$zip->addFromString(basename($file), $download_file);
		}
		$newFile = $filerecords[$k];
		$k++;
	}
	// close zip
	$zip->close();

	// send the file to the browser as a download zipped folder
	header('Content-disposition: attachment; filename='.$zipFolderName.'.zip');
	header('Content-type: application/zip');
	readfile($tmp_file);
}
}
?>

How to create Zip folder and download in Cake Php

Click here to know Aaj ka Suvichar in Hindi

Leave a Reply

Your email address will not be published. Required fields are marked *