Skip to content

Instantly share code, notes, and snippets.

@tcarlsen
Created June 24, 2010 09:01
Show Gist options
  • Save tcarlsen/451192 to your computer and use it in GitHub Desktop.
Save tcarlsen/451192 to your computer and use it in GitHub Desktop.
<?php
class DownloaderComponent extends Object {
function download_file($file) {
$basename = basename($file);
$file_extension = strtolower(substr(strrchr($basename,"."),1));
switch($file_extension)
{
case "PLS": $ctype="audio/x-scpls"; break;
case "pls": $ctype="audio/x-scpls"; break;
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $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); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"$basename\";" );
header("Content-Transfer-Encoding: binary");
readfile($file);
exit();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment