关于php支持分块与断点续传文件下载功能代码
程序员文章站
2022-07-21 23:53:28
本文章要介绍了这篇文章是一篇关于php流下载,就是可以支持分块与断点续传文件下载,有需要的朋友可以看看。代码如下 复制代码 代码如下: $dowmfile = dirnam...
本文章要介绍了这篇文章是一篇关于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;
}
}
复制代码 代码如下:
$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;
}
}