PHP remote file download class (supports resumable upload)

Source: Internet
Author: User
PHP remote file download class (supports resumable Upload), read PHP remote file download class (supports resumable Upload), 1. function: supports resumable download, can calculate the transfer rate, can control the transfer rate easy to use: $ object = newhttpdownload (); $ object-set_byfile ($ file) % N # H # %; // server file name, including Path $ object-fil

1. function: supports resumable download, computing the transfer rate, and controlling the transfer rate

Easy to use:

$ Object = new httpdownload ();
$ Object-> set_byfile ($ file) % N # H # %; // server file name, including Path
$ Object-> filename = $ filename; // download the file name saved
$ Object-> download ();

3. source file:

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 seek end here
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; // you must use a handler
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.