PHP implements resumable download, and php implements resumable download.

Source: Internet
Author: User
Tags ranges

PHP implements resumable download, and php implements resumable download.

This example describes how to implement resumable download in PHP. Share it with you for your reference.

The specific implementation code is as follows:
Copy codeThe Code is as follows: <? Php
/*
* PHP downloads resumable upload
*/
Function dl_file_resume ($ file ){
 
// Check whether the file exists
If (! Is_file ($ file) {die ("<B> 404 File not found! </B> ");}

$ Len = filesize ($ file); // get the file size
$ Filename = basename ($ file); // get the file name
$ File_extension = strtolower (substr (strrchr ($ filename, "."), 1); // get the file extension

// Specify the output browser format based on the extension
Switch ($ file_extension ){
Case "exe": $ ctype = "application/octet-stream"; break;
Case "zip": $ ctype = "application/zip"; break;
Case "mp3": $ ctype = "audio/mpeg"; break;
Case "mpg": $ ctype = "video/mpeg"; break;
Case "avi": $ ctype = "video/x-msvideo"; break;
Default: $ ctype = "application/force-download ";
}

// Begin writing headers
Header ("Cache-Control :");
Header ("Cache-Control: public ");

// Set the output browser format
Header ("Content-Type: $ ctype ");
If (strstr ($ _ SERVER ['HTTP _ USER_AGENT '], "MSIE") {// if it is an IE browser
# Workaround for IE filename bug with multiple periods/multiple dots in filename
# That adds square brackets to filename-eg. setup.abc.exe becomes setup000012.16.abc.exe
$ Iefilename = preg_replace ('/\./', '% 2e', $ filename, substr_count ($ filename,'. ')-1 );
Header ("Content-Disposition: attachment; filename = \" $ iefilename \"");
} Else {
Header ("Content-Disposition: attachment; filename = \" $ filename \"");
}
Header ("Accept-Ranges: bytes ");

$ Size = filesize ($ file );
// If $ _ SERVER ['HTTP _ range'] parameter exists
If (isset ($ _ SERVER ['HTTP _ range']) {
/*---------------------------
The Range header field can request one or more sub-ranges of an object. For example, it indicates the first 500 bytes: bytes = 0-499 indicates the second 500 bytes: bytes = 500-999 indicates the last 500 bytes: bytes =-500 indicates the range after 500 bytes: bytes = 500-first and last bytes: bytes = 0-0,-1 specifies the range at the same time: bytes = 500-600,601-999 but the server can ignore this request header. If the unconditional GET contains the Range request header, the response will be returned with the status code 206 (PartialContent) instead of 200 (OK ).
---------------------------*/

// The value of connecting to $ _ SERVER ['HTTP _ range'] again after the breakpoint is bytes = 4390912-

List ($ a, $ range) = explode ("=", $ _ SERVER ['HTTP _ range']);
// If yes, download missing part
Str_replace ($ range, "-", $ range); // What is this sentence ....
$ Size2 = $ size-1; // total file bytes
$ New_length = $ size2-$ range; // obtain the length of the next download.
Header ("HTTP/1.1 206 Partial Content ");
Header ("Content-Length: $ new_length"); // The total Length of input
Header ("Content-Range: bytes $ range $ size2/$ size"); // Content-Range: bytes 4908618-4988927/4988928 95%
} Else {// The first connection
$ Size2 = $ size-1;
Header ("Content-Range: bytes 0-$ size2/$ size"); // Content-Range: bytes 0-4988927/4988928
Header ("Content-Length:". $ size); // total output Length
}
// Open the file
$ Fp = fopen ("$ file", "rb ");
// Set the pointer position
Fseek ($ fp, $ range );
// Unreal output
While (! Feof ($ fp )){
// Set the maximum file execution time
Set_time_limit (0 );
Print (fread ($ fp, 1024*8); // output file
Flush (); // output buffer
Ob_flush ();
}
Fclose ($ fp );
Exit;
}
 
Dl_file_resume ("1.zip"); // 1.zip file of the same level directory
 
//---------------------------------------
 
// Resumable download is not supported.
 
//---------------------------------------

DownFile ("1.zip ");
 
Function downFile ($ sFilePath)
{
If (file_exists ($ sFilePath )){
$ AFilePath = explode ("/", str_replace ("\", "/", $ sFilePath), $ sFilePath );
$ SFileName = $ aFilePath [count ($ aFilePath)-1];
$ NFileSize = filesize ($ sFilePath );
Header ("Content-Disposition: attachment; filename =". $ sFileName );
Header ("Content-Length:". $ nFileSize );
Header ("Content-type: application/octet-stream ");
Readfile ($ sFilePath );
}
Else
{
Echo ("the file does not exist! ");
}
}
?>

I hope this article will help you with PHP programming.

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.