function download()
{
if (!$this->initialize()) {
return FALSE;
}
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;
//do some clean up
if (ob_get_length() !== FALSE) {
@ob_end_clean();
}
$old_status = ignore_user_abort(TRUE);
if (!ini_get('safe_mode') && function_exists("set_time_limit")) {
@set_time_limit(0);
}
$this->bandwidth = 0;
$size = $this->data_len;
if ($this->data_type == 0) {
$size = filesize($this->data);
if ($seek > $size - 1) {
$seek = 0;
}
if ($this->filename == NULL) {
$this->filename = basename($this->data);
}
$res = fopen($this->data, 'rb');
if ($seek) {
fseek($res, $seek);
}
if ($this->seek_end < $seek) {
$this->seek_end = $size - 1;
}
$this->header($size, $seek, $this->seek_end);
//always use the last seek
$size = $this->seek_end - $seek + 1;
while (!(connection_aborted() || connection_status() == 1) && $size > 0) {
if ($size < $bufsize) {
echo fread($res, $size);
$this->bandwidth += $size;
} else {
echo fread($res, $bufsize);
$this->bandwidth += $bufsize;
}
$size -= $bufsize;
flush();
if ($speed > 0 && $this->bandwidth > $speed * $packet * 1024) {
sleep(1);
$packet++;
}
}
fclose($res);
} elseif ($this->data_type == 1) {
if ($seek > $size - 1) {
$seek = 0;
}
if ($this->seek_end < $seek) {
$this->seek_end = $this->data_len - 1;
}
$this->data = substr($this->data, $seek, $this->seek_end - $seek + 1);
if ($this->filename == NULL) {
$this->filename = time();
}
$size = strlen($this->data);
$this->header($this->data_len, $seek, $this->seek_end);
while (!connection_aborted() && $size > 0) {
if ($size < $bufsize) {
$this->bandwidth += $size;
} else {
$this->bandwidth += $bufsize;
}
echo substr($this->data, 0, $bufsize);
$this->data = substr($this->data, $bufsize);
$size -= $bufsize;
flush();
if ($speed > 0 && $this->bandwidth > $speed * $packet * 1024) {
sleep(1);
$packet++;
}
}
} else {
if ($this->data_type == 2) {
//just send a redirect header
header('location: ' . $this->data);
}
}
if ($this->use_autoexit) {
exit;
}
//restore old status
ignore_user_abort($old_status);
if (!ini_get('safe_mode') && function_exists("set_time_limit")) {
@set_time_limit(ini_get("max_execution_time"));
}
return TRUE;
}