Skip to content

Instantly share code, notes, and snippets.

@somatonic
Created May 23, 2012 15:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save somatonic/2775882 to your computer and use it in GitHub Desktop.
Save somatonic/2775882 to your computer and use it in GitHub Desktop.
Download PHP Class
<?php
class download{
public function startDownload( $vFilePath, $vDownloadName=""){
$vFilename = basename( $vFilePath);
$vNewFilename = $vDownloadName == "" ? $vFilename : $vDownloadName;
$vFileType = $this->getFileType( $vFilename);
$vContentType = $this->GetContentType( $vFileType);
// Fix IE bug [0]
$header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $vNewFilename, substr_count($vNewFilename, '.') - 1) : $vNewFilename;
// Prepare headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Content-Type: " . $vContentType);
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=\"" . $header_file . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($vFilePath));
set_time_limit(0);
// download mittels readfile funktioniert nicht bei grossen dateien
/* datei paeckchenweise lesen und in den buffer schreiben */
$vBlockSize = 1048576;//1024;
$vDownlSpeed= 10;
$dlfile= fopen( $vFilePath, 'r');
while (!feof($dlfile) && connection_status() == 0){
//reset time limit for big files
set_time_limit(0);
print fread( $dlfile, $vBlockSize*$vDownlSpeed);
flush();
}
fclose( $dlfile);
exit();
}
function getFileType( $vFilename){
return strtolower(substr(strrchr($vFilename,"."),1));
}
function GetBrowserId(){
$browser= $_SERVER['HTTP_USER_AGENT'];
if(ereg('Opera(/| )([0-9].[0-9]{1,2})', $browser)) {
return 'OPERA';
} else if(ereg('MSIE ([0-9].[0-9]{1,2})', $browser)) {
return 'IE';
} else if(ereg('OmniWeb/([0-9].[0-9]{1,2})', $browser)) {
return 'OMNIWEB';
} else if(ereg('(Konqueror/)(.*)', $browser)) {
return 'KONQUEROR';
} else if(ereg('Mozilla/([0-9].[0-9]{1,2})', $browser)) {
return 'MOZILLA';
} else {
return 'OTHER';
}
}
function GetContentType( $FileType=""){
$vBrowser= $this->GetBrowserId();
switch ($FileType) {
case "exe": (($vBrowser=='IE' || $vBrowser=='OPERA') ? ($ctype="application/octetstream") : ($ctype="application/octet-stream")); break;
case "pdf": $ctype="application/pdf"; 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 "wmv": $ctype="video/x-ms-wmv"; break;
case "jpe": case "jpeg": case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download"; break;
}
return $ctype;
}
}
// sample usage
$cDownload= new download();
$cDownload->startDownload( $_SERVER['DOCUMENT_ROOT'] . $page->pdf->url );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment