PHP Remote File Download class (supports resumable upload)

Source: Internet
Author: User
Tags fread

Easy to use: CopyCode The Code is as follows: $ 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: Copy code The Code is 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 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.