file downlaod code at cakephp


This code help you to download any file from server

function download($filename) {
$filename = WWW_ROOT . DS . $filename;
if (file_exists($filename)) {
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/force-download”);
header(“Content-Type: application/zip”);
header(“Content-Type: application/pdf”);
header(“Content-Type: application/octet-stream”);
header(“Content-Type: image/png”);
header(“Content-Type: image/gif”);
header(“Content-Type: image/jpg”);
header(“Content-Type: application/vnd.ms-excel”);
header(“Content-Type: application/vnd.ms-excel”);
header(“Content-Type: application/vnd.ms-powerpoint”);
header(“Content-type: application/x-msexcel”);
header(“Content-type: application/msword”);
header(“Content-Description: File Transfer”);
header(“Content-Disposition: attachment; filename=” . basename($filename) . “;”);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: ” . filesize($filename));
readfile($filename) or die(‘Errors’);
exit(0);
}
}

Another Example:

function download_material($id = null){
$material = $this->ExhibitorMaterial->read(null,$id);
$filename = WWW_ROOT . ‘exhibitor_material’ . DS . $material[‘ExhibitorMaterial’][‘file_type’];
if (file_exists($filename)) {
//Parse file get extension and size
$fsize = filesize($filename);
$path_parts = pathinfo($filename);
$ext = strtolower($path_parts[“extension”]);
// Determine Content Type
switch ($ext) {
case “pdf”:
$ctype = “application/pdf”;
break;
case “exe”:
$ctype = “application/octet-stream”;
break;
case “zip”:
case “rar”:
$ctype = “application/zip”;
break;
case “doc”:
case “docx”:
$ctype = “application/msword”;
break;
case “xls”:
case “xlsx”:
$ctype = “application/vnd.ms-excel”;
break;
case “ppt”:
case “pptx”:
$ctype = “application/vnd.ms-powerpoint”;
break;
case “gif”:
$ctype = “image/gif”;
break;
case “png”:
$ctype = “image/png”;
break;
case “jpeg”:
case “jpg”:
$ctype = “image/jpg”;
break;
default: $ctype = “application/force-download”;
}
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: $ctype”);
header(“Content-Description: File Transfer”);
header(“Content-Disposition: attachment; filename=” . basename($filename) . “;”);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: ” . $fsize);
readfile($filename) or die(‘Errors’);
exit(0);
}
}

Advertisements

One response to “file downlaod code at cakephp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s