Code about php support for multipart and resumable file download

Source: Internet
Author: User
Php stream download supports multipart and resumable Download. if you have any need, read this article to introduce this article, which is an article about php stream download, it supports multipart and resumable Download. if you need it, you can check it. The code is as follows:

The code is as follows:


$ DowmFile = dirname (_ FILE _). '/Nokia-Always Here.mp3'; // the object to be downloaded, either absolute or relative
$ DowmName = 'Nokia-Always Here.mp3 ′;
Ob_start ();
Getlocalfile ($ dowmFile, $ dowmName );
Flush ();
Ob_flush ();
Function getlocalfile ($ fname, $ filename = "){
$ Fsize = filesize ($ fname );
Header ('cache-Control: public ');
Header ('pragma: public ');
Header ('Accept-Ranges: bytes ');
Header ('connection: close ');
Header ('content-Type: '. MIMEType ($ fname ));
// Header ('content-Type: application/octet-stream ');
If (isset ($ filename {0 })){
Header ('content-Disposition: attachment; filename = '. $ filename );
}
If ($ fp = @ fopen ($ fname, 'RB ')){
$ Start = 0;
$ End = $ fsize;
$ IsRange = isset ($ _ SERVER ['http _ range']) & ($ _ SERVER ['http _ range']! = ");
If ($ isRange ){
Preg_match ('/^ bytes = ([0-9] *)-([0-9] *) $/I', $ _ SERVER ['http _ range'], $ match );
$ Start = $ match [1];
$ End = $ match [2];
$ Isset_start = isset ($ start {0 });
$ Isset_end = isset ($ end {0 });
If ($ isset_start & $ isset_end ){
// Multipart download
If ($ start >=$ fsize | $ start <0 | $ start> $ end ){
$ Start = 0;
$ End = $ fsize;
} Else if ($ end >=$ fsize ){
$ End = $ fsize-$ start;
} Else {
$ End-= $ start-1;
}
} Else if ($ isset_start &&! $ Isset_end ){
// Specify the position to end
If ($ start >=$ fsize | $ start <0 ){
$ Start = 0;
$ End = $ fsize;
} Else {
$ End = $ fsize-$ start;
}
} Else if (! $ Isset_start & $ isset_end ){
// Last n bytes
$ End = $ end> $ fsize? $ Fsize: $ end;
$ Start = $ fsize-$ end;
} Else {
$ Start = 0;
$ End = $ fsize;
}
}
If ($ isRange ){
Fseek ($ fp, $ start );
Header ('http/1.1 206 Partial content ');
Header ('content-Length: '. $ end );
Header ('content-Ranges: bytes '. $ start.'-'. ($ end + $ start-1).'/'. $ fsize );
} Else {
Header ('content-Length: '. $ fsize );
}
If (function_exists ('fpassthru') & ($ end + $ start) ==$ fsize ){
Fpassthru ($ fp );
} Else {
Echo fread ($ fp, $ end );
}
} Else {
Header ('content-Length: '. $ fsize );
Readfile ($ fname );
}
// @ Header ("Content-Type:". mime_content_type ($ fname ));
}
Function MIMEType ($ fname ){
$ FileSuffix = strtolower (substr ($ fname, strrpos ($ fname, '.') + 1 ));
Switch ($ fileSuffix ){
Case 'Av ':
Return 'video/msvideo ';
Case 'vps ':
Return 'video/x-ms-wmv ';
Case 'txt ':
Return 'text/plain ';
Case 'htm ':
Case 'html ':
Case 'php ':
Return 'text/html ';
Case 'css ':
Return 'text/css ';
Case 'js ':
Return 'application/javascript ';
Case 'json ':
Case 'xml ':
Case 'Zip ':
Case 'PDF ':
Case 'rtf ':
Case 'tar ':
Return 'application/'. $ fileSuffix;
Case 'swf ':
Return 'application/x-shockwave-Flash ';
Case 'flv ':
Return 'video/x-flv ';
Case 'jpe ':
Case 'jpg ':
Return 'image/jpeg ';
Case 'jpeg ':
Case 'PNG ':
Case 'GIF ':
Case 'bmp ':
Case 'Tiff ':
Return 'image/'. $ fileSuffix;
Case 'ico ':
Return 'image/vnd. microsoft. icon ';
Case 'tif ':
Return 'image/tiff ';
Case 'svg ':
Case 'svgz ':
Return 'image/svg + XML ';
Case 'rar ':
Return 'application/x-rar-compressed ';
Case 'exe ':
Case 'msi ':
Return 'application/x-msdownload ';
Case 'cab ':
Return 'application/vnd. ms-cab-compressed ';
Case 'aif ':
Return 'audio/aiff ';
Case 'mpg ':
Case 'mpe ':
Case 'mp3 ′:
Return 'audio/mpeg ';
Case 'MPEG ':
Case 'wav ':
Case 'aiff ':
Return 'audio/'. $ fileSuffix;
Case 'qt ':
Case 'mov ':
Return 'video/quicktime ';
Case 'psd ':
Return 'image/vnd. adobe. photoshop ';
Case 'AI ':
Case 'eps ':
Case 'Ps ':
Return 'application/postscript ';
Case 'Doc ':
Case 'docx ':
Return 'application/msword ';
Case 'XLS ':
Case 'xlt ':
Case 'xlm ':
Case 'xld ':
Case 'xla ':
Case 'xlc ':
Case 'xlw ':
Case 'xll ':
Return 'application/vnd. ms-excel ';
Case 'ppt ':
Case 'pps ':
Return 'application/vnd. ms-powerpoint ';
Case 'odt ':
Return 'application/vnd. oasis. opendocument. text ';
Case 'ods ':
Return 'application/vnd. oasis. opendocument. spreadsheet ';
Default:
If (function_exists ('Mime _ content_type ')){
$ FileSuffix = mime_content_type ($ filename );
} Else {
$ FileSuffix = 'application/octet-stream ';
}
Return $ fileSuffix;
Break;
}
}

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.