PHP supports chunked and breakpoint continuation file download function code _php Tutorial

Source: Internet
Author: User
Tags ranges
This article is to introduce this article is an article on the PHP stream download, that is, you can support the block and the continuation of the breakpoint file downloads, there is a need for friends to see.
The code is as follows Copy Code

$dowmFile = DirName (__file__). '/nokia-always Here.mp3 '; Files to be downloaded, 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) {
Sub-block 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 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 ' avi ':
Return ' Video/msvideo ';
Case ' WMV ':
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
}
}


http://www.bkjia.com/PHPjc/444707.html www.bkjia.com true http://www.bkjia.com/PHPjc/444707.html techarticle This article is to introduce this article is an article on the PHP stream download, that is, you can support the block and the continuation of the breakpoint file downloads, there is a need for friends to see. The code below copies the code $dow ...

  • Related Article

    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.