class download var properties array old_name new_name type size resume

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class download{
var $properties = array( 'old_name' => "",
'new_name' => "",
'type' => "",
'size' => "",
'resume' => "",
'max_speed' => ""
);
var $range = 0;
function download($path, $name="", $resume=0, $max_speed=0){
$name = ($name == "") ? substr(strrchr("/".$path,"/"),1) : $name;
$name = explode("/", $name);
$name = end ($name);
$file_size = @filesize($path);
$this->properties = array(
'old_name' => $path,
'new_name' => $name,
'type'=> "application/force-download",
'size' => $file_size,
'resume' => $resume,
'max_speed' => $max_speed
);
if ($this->properties['resume']) {
if(isset($_SERVER['HTTP_RANGE'])) {
$this->range = $_SERVER['HTTP_RANGE'];
$this->range = str_replace("bytes=", "", $this->range);
$this->range = str_replace("-", "", $this->range);
} else {
$this->range = 0;
}
if ($this->range > $this->properties['size']) $this->range = 0;
} else {
$this->range = 0;
}
}
function download_file(){
if ($this->range) {
header($_SERVER['SERVER_PROTOCOL']." 206 Partial Content");
} else {
header($_SERVER['SERVER_PROTOCOL']." 200 OK");
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: ".$this->properties["type"]);
header('Content-Disposition: attachment; filename="'.$this->properties['new_name'].'";');
header("Content-Transfer-Encoding: binary");
if ($this->properties['resume']) header("Accept-Ranges: bytes");
if ($this->range) {
header("Content-Range: bytes {$this->range}-".($this->properties['size']-1)."/".$this->properties['size']);
header("Content-Length: ".($this->properties['size']-$this->range));
} else {
header("Content-Length: ".$this->properties['size']);
}
@ini_set('max_execution_time', 0);
@set_time_limit();
$this->_download($this->properties['old_name'], $this->range);
}
function _download ($filename, $range=0)
{
@ob_end_clean();
if (($speed = $this->properties['max_speed']) > 0)
$sleep_time = (8 / $speed) * 1e6;
else
$sleep_time = 0;
$handle = fopen($filename, 'rb');
fseek($handle,$range);
if ($handle === false)
{
return false;
}
while (!feof($handle))
{
print (fread($handle, 1024*8));
ob_flush();
flush();
usleep($sleep_time);
}
fclose($handle);
return true;
}
}