About PHP support block and breakpoint resume file download function code _php tips

Source: Internet
Author: User
Tags ranges rar
This article is to introduce this article is about PHP streaming, that is, can support the block and breakpoint file download, there is a need for friends to see. The code is as follows
Copy Code code as follows:

$dowmFile = DirName (__file__). '/nokia–always here.mp3′; To download the file, 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) {
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 location 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
}
}

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.