Easy to use method:
Copy Code code as follows:
$object = new Httpdownload ();
$object->set_byfile ($file)%n#h#%;//server file name, including path
$object->filename = $filename;//download Save as file name
$object->download ();
3. Source Files:
Copy Code code as follows:
?
Class Httpdownload {
var $data = null;
var $data _len = 0;
var $data _mod = 0;
var $data _type = 0;
var $data _section = 0; Section download
var $sentSize = 0;
var $handler = array (' auth ' => null);
var $use _resume = true;
var $use _autoexit = false;
var $use _auth = false;
var $filename = null;
var $mime = null;
var $bufsize = 2048;
var $seek _start = 0;
var $seek _end =-1;
var $totalsizeref = 0;
var $bandwidth = 0;
var $speed = 0;
function Initialize () {
Global $HTTP _server_vars;
if ($this->use_auth)//use authentication
{
if (! $this->_auth ())//no authentication
{
Header (' Www-authenticate:basic realm= ' Please enter your username and password ');
Header (' http/1.0 401 Unauthorized ');
Header (' status:401 unauthorized ');
if ($this->use_autoexit) exit ();
return false;
}
}
if ($this->mime = null) $this->mime = "Application/octet-stream"; Default MIME
if (Isset ($_server[' Http_range ')) | | | isset ($HTTP _server_vars[' Http_range '])
{
if (isset ($HTTP _server_vars[' Http_range ')) $seek _range = substr ($HTTP _server_vars[' Http_range '), strlen (' bytes= ')) ;
else $seek _range = substr ($_server[' Http_range '), strlen (' bytes= '));
$range = Explode ('-', $seek _range);
if ($range [0] > 0)
{
$this->seek_start = intval ($range [0]);
}
if ($range [1] > 0) $this->seek_end = Intval ($range [1]);
else $this->seek_end =-1;
if (! $this->use_resume)
{
$this->seek_start = 0;
Header ("http/1.0 404 Bad Request");
Header ("status:400 bad Request");
Exit
return false;
}
Else
{
$this->data_section = 1;
}
}
Else
{
$this->seek_start = 0;
$this->seek_end =-1;
}
$this->sentsize=0;
return true;
}
function header ($size, $seek _start=null, $seek _end=null) {
Header (' Content-type: '. $this->mime);
Header (' Content-disposition:attachment filename= "'. $this->filename. '"');
Header (' last-modified: '. Date (' d, D M Y h:i:s \g\m\t ', $this->data_mod));
if ($this->data_section && $this->use_resume)
{
Header ("http/1.0 206 Partial Content");
Header ("status:206 Partial Content");
Header (' accept-ranges:bytes ');
Header ("Content-range:bytes $seek _start-$seek _end/$size");
Header ("Content-length:"). ($seek _end-$seek _start + 1));
}
Else
{
Header ("Content-length: $size");
}
}
function download_ex ($size)
{
if (! $this->initialize ()) return false;
Ignore_user_abort (TRUE);
Use to
if ($this->seek_start > ($size-1)) $this->seek_start = 0;
if ($this->seek_end <= 0) $this->seek_end = $size-1;
$this->header ($size, $seek, $this->seek_end);
$this->data_mod = time ();
return true;
}
function Download () {
if (! $this->initialize ()) return false;
Try
{
Error_log ("Begin download\n", 3, "/usr/local/www/apache22/logs/apache22_php.err");
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;
Do some clean up
@ob_end_clean ();
$old _status = Ignore_user_abort (true);
@set_time_limit (0);
$this->bandwidth = 0;
$size = $this->data_len;
if ($this->data_type = = 0)//download from a file
{
$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;
$this->sentsize+= $size;
}
Else
{
Echo fread ($res, $bufsize);
$this->bandwidth + + $bufsize;
$this->sentsize+= $bufsize;
}
$size-= $bufsize;
Flush ();
if ($speed > 0 && ($this->bandwidth > $speed * $packet *1024))
{
Sleep (1);
$packet + +;
}
}
Fclose ($res);
}
ElseIf ($this->data_type = = 1)//download from a string
{
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;
$this->sentsize+= $size;
}
Else
{
$this->bandwidth + + $bufsize;
$this->sentsize+= $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->totalsizeref== $this->sentsize) error_log ("End download\n", 3, "/usr/local/www/apache22/logs/ Apache22_php.err ");
else Error_log ("Download is canceled\n", 3, "/usr/local/www/apache22/logs/apache22_php.err");
if ($this->use_autoexit) exit ();
Restore Old status
Ignore_user_abort ($old _status);
Set_time_limit (Ini_get ("Max_execution_time"));
}
catch (Exception $e)
{
Error_log ("Cancel download\n". $e, 3, "/usr/local/www/apache22/logs/apache22_php.err");
}
return true;
}
function Set_byfile ($dir) {
if (is_readable ($dir) && is_file ($dir)) {
$this->data_len = 0;
$this->data = $dir;
$this->data_type = 0;
$this->data_mod = Filemtime ($dir);
$this->totalsizeref = filesize ($dir);
return true;
else return false;
}
function Set_bydata ($data) {
if ($data = = ") return false;
$this->data = $data;
$this->data_len = strlen ($data);
$this->data_type = 1;
$this->data_mod = time ();
return true;
}
function Set_byurl ($data) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
return true;
}
function Set_lastmodtime ($time) {
$time = Intval ($time);
if ($time <= 0) $time = time ();
$this->data_mod = $time;
}
function _auth () {
if (!isset ($_server[' Php_auth_user ')) return false;
if (isset ($this->handler[' auth ']) && function_exists ($this->handler[' auth '))
{
return $this->handler[' auth ' (' auth ', $_server[' php_auth_user '],$_server[' PHP_AUTH_PW ']);
}
else return true; Must use a handler
}
}
?>