<?php
class downloader {
private $file;
private function get_mime_type($file = false) {
$type = $file ? explode('.',basename($this->file)) : explode('.',basename($file));
$file_type = $type[count($type)-1];
$mimes = array('psd' => 'application/octet-stream',
'pdf' => 'application/pdf',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'zip' => 'application/zip',
'gzip' => 'application/x-gzip',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'txt' => 'text/plain',
'html' => 'text/html',
'doc' => 'application/msword',
'xl' => 'application/excel',
'xls' => 'application/excel',
'mov' => 'video/quicktime',
'qt' => 'video/quicktime',
'mpg' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'mp3' => 'audio/mpeg',
'aiff' => 'audio/x-aiff',
'aif' => 'audio/x-aiff',
'aac' => 'audio/aac',
'flv' => 'video/x-flv'
);
return array_key_exists($file_type,$mimes) ? $mimes[$file_type] : 'application/octet-stream';
}
public function force_download($file, $newname = false, $del_puffer = false) {
if (file_exists($file)) {
$this->file = $file;
if ($del_puffer) ob_end_clean();
header('Content-Transfer-Encoding: none');
header("Content-Type: ".$this->get_mime_type($newname));
header( 'Content-Length: ' .filesize($this->file));
$name = !$newname ? basename($this->file) : $newname;
header( 'Content-Disposition: attachment; filename="'.$name.'"');
$fp = fopen($this->file, 'rb');
while (!feof($fp)) {
set_time_limit(30);
$buffer = fread($fp, 1024);
echo $buffer;
}
exit;
}
else return false;
}
}
?> |