Posts Tagged ‘download’

Batch download in zip format

Dealing with files uploaded to the server, users might want to download the files in one zip file. So this snippet of code is the one that I have been using in my Zend Framework action:

$compressedFileName = 'download'.mktime().'.zip';
$currentFolderPath = '/data/';
$compressedFileLocation = '/tmp/'.$compressedFileName;
foreach ($files as $file) {
	$files_to_be_compressed[] = '"'.$file->getFileName().'"';
if (count($files_to_be_compressed)) {
	$command = "zip $compressedFileName " . implode(' ', $files_to_be_compressed);
//move the tar file to temp folder so that we can easily remove all the temp tar files with cron
	$command = "mv $compressedFileName $compressedFileLocation";
if (file_exists($compressedFileLocation))
	//disable the default layout because we want to present a file
	$documentMime = $docmans->mime_content_type($compressedFileLocation);
	// Since everything is correct, send the file then!
	header('Content-Description: File Transfer');
	header("Content-Type: $documentMime");
	header("Content-Length: ".filesize($compressedFileLocation));
	header("Content-Disposition: attachment; filename=\"$compressedFileName\"");
	header('Content-Transfer-Encoding: binary');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: public');

This will only work in linux server, I guess.