关于php支持分块与断点续传文件下载功能代码

yipeiwu_com5年前PHP代码库
本文章要介绍了这篇文章是一篇关于php流下载,就是可以支持分块与断点续传文件下载,有需要的朋友可以看看。代码如下
复制代码 代码如下:

$dowmFile = dirname ( __FILE__ ) . ‘/Nokia – Always Here.mp3′; //要下载的文件,绝对或相对
$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) {
//分块下载
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) {
//指定位置到结束
if ($start >= $fsize || $start < 0) {
$start = 0;
$end = $fsize;
} else {
$end = $fsize – $start;
}
} else if (! $isset_start && $isset_end) {
//最后n个字节
$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;
}
}

相关文章

php 操作数组(合并,拆分,追加,查找,删除等)

1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组。所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加。其形式为: 复制代码 代码如下...

PHP代码优化之成员变量获取速度对比

有如下4个代码示例,你认为他们创建对象,并且获得成员变量的速度排序是怎样的? 1:将成员变量设置为public,通过赋值操作给成员变量赋值,直接获取变量复制代码 代码如下:<?ph...

关于php正则匹配汉字的方法介绍

php正则匹配汉字! /^[\x{4e00}-\x{9fa5}]+$/u以上这个正则表达式就是困扰了很多php程序员的匹配汉字的正则表达式大家可能会觉得很简单,实际上不同编码,不同程...

Sorting Array Values in PHP(数组排序)

复制代码 代码如下: $full_name = array(); $full_name["Roger"] = "Waters"; $full_name["Richard"] = "Wri...

PHP中一个有趣的preg_replace函数详解

PHP中一个有趣的preg_replace函数详解

0x01 起因 事情的起因是下午遇到了 preg_replace 函数,我们都知道 preg_replace 函数可能会导致命令执行。现在我们来一些情况。 0x02 经过 踩坑1:...