How to create Zip folder and download in Cake PHP

How to create Zip folder and download in Cake PHP

How to create Zip folder and download in Cake PHP (केक PHP में ज़िप फ़ोल्डर कैसे बनाएँ और डाउनलोड करें)-

Step : 1 – First create Hyperlink for download zip folder in studentdetail.ctp file and add bellow codes (सबसे पहले studentdetail.ctp फ़ाइल में डाउनलोड ज़िप फ़ोल्डर के लिए हाइपरलिंक बनाएं और नीचे दिए गए कोड जोड़ें) –

<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 (“downloadzipdata” StudentController.php का फ़ंक्शन/क्रिया नाम है)

Step : 2 – Create RegistrationDetails.php PHP file Model and bellow codes (RegistrationDetails.php PHP फ़ाइल मॉडल और नीचे दिए गए कोड बनाएँ)-

<?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 (StudentController.php बनाएं तथा नीचे दिए गए कोड जोड़ें)-

<?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);
}
}
?>

Click here to know Aaj ka Suvichar in Hindi

How to create Zip folder and download in Cake PHP

Tags: How to create zip file using PHP, How to Create Zip Files using PHP and Download, How to create Zip folder and download in Cake Php, Zip Component in CakePHP

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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