關(guān)于php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼
更新時(shí)間:2014年05月09日 09:40:19 作者:
一篇關(guān)于php流下載,就是可以支持分塊與斷點(diǎn)續(xù)傳文件下載,有需要的朋友可以看看
本文章要介紹了這篇文章是一篇關(guān)于php流下載,就是可以支持分塊與斷點(diǎn)續(xù)傳文件下載,有需要的朋友可以看看。代碼如下
$dowmFile = dirname ( __FILE__ ) . ‘/Nokia – Always Here.mp3′; //要下載的文件,絕對(duì)或相對(duì)
$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) {
//指定位置到結(jié)束
if ($start >= $fsize || $start < 0) {
$start = 0;
$end = $fsize;
} else {
$end = $fsize – $start;
}
} else if (! $isset_start && $isset_end) {
//最后n個(gè)字節(jié)
$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 ‘a(chǎn)vi' :
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 ‘a(chǎn)pplication/javascript';
case ‘json' :
case ‘xml' :
case ‘zip' :
case ‘pdf' :
case ‘rtf' :
case ‘tar' :
return ‘a(chǎn)pplication/' . $fileSuffix;
case ‘swf' :
return ‘a(chǎn)pplication/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 ‘a(chǎn)pplication/x-rar-compressed';
case ‘exe' :
case ‘msi' :
return ‘a(chǎn)pplication/x-msdownload';
case ‘cab' :
return ‘a(chǎn)pplication/vnd.ms-cab-compressed';
case ‘a(chǎn)if' :
return ‘a(chǎn)udio/aiff';
case ‘mpg' :
case ‘mpe' :
case ‘mp3′ :
return ‘a(chǎn)udio/mpeg';
case ‘mpeg' :
case ‘wav' :
case ‘a(chǎn)iff' :
return ‘a(chǎn)udio/' . $fileSuffix;
case ‘qt' :
case ‘mov' :
return ‘video/quicktime';
case ‘psd' :
return ‘image/vnd.adobe.photoshop';
case ‘a(chǎn)i' :
case ‘eps' :
case ‘ps' :
return ‘a(chǎn)pplication/postscript';
case ‘doc' :
case ‘docx' :
return ‘a(chǎn)pplication/msword';
case ‘xls' :
case ‘xlt' :
case ‘xlm' :
case ‘xld' :
case ‘xla' :
case ‘xlc' :
case ‘xlw' :
case ‘xll' :
return ‘a(chǎn)pplication/vnd.ms-excel';
case ‘ppt' :
case ‘pps' :
return ‘a(chǎn)pplication/vnd.ms-powerpoint';
case ‘odt' :
return ‘a(chǎn)pplication/vnd.oasis.opendocument.text';
case ‘ods' :
return ‘a(chǎn)pplication/vnd.oasis.opendocument.spreadsheet';
default :
if (function_exists ( ‘mime_content_type' )) {
$fileSuffix = mime_content_type ( $filename );
} else {
$fileSuffix = ‘a(chǎn)pplication/octet-stream';
}
return $fileSuffix;
break;
}
}
復(fù)制代碼 代碼如下:
$dowmFile = dirname ( __FILE__ ) . ‘/Nokia – Always Here.mp3′; //要下載的文件,絕對(duì)或相對(duì)
$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) {
//指定位置到結(jié)束
if ($start >= $fsize || $start < 0) {
$start = 0;
$end = $fsize;
} else {
$end = $fsize – $start;
}
} else if (! $isset_start && $isset_end) {
//最后n個(gè)字節(jié)
$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 ‘a(chǎn)vi' :
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 ‘a(chǎn)pplication/javascript';
case ‘json' :
case ‘xml' :
case ‘zip' :
case ‘pdf' :
case ‘rtf' :
case ‘tar' :
return ‘a(chǎn)pplication/' . $fileSuffix;
case ‘swf' :
return ‘a(chǎn)pplication/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 ‘a(chǎn)pplication/x-rar-compressed';
case ‘exe' :
case ‘msi' :
return ‘a(chǎn)pplication/x-msdownload';
case ‘cab' :
return ‘a(chǎn)pplication/vnd.ms-cab-compressed';
case ‘a(chǎn)if' :
return ‘a(chǎn)udio/aiff';
case ‘mpg' :
case ‘mpe' :
case ‘mp3′ :
return ‘a(chǎn)udio/mpeg';
case ‘mpeg' :
case ‘wav' :
case ‘a(chǎn)iff' :
return ‘a(chǎn)udio/' . $fileSuffix;
case ‘qt' :
case ‘mov' :
return ‘video/quicktime';
case ‘psd' :
return ‘image/vnd.adobe.photoshop';
case ‘a(chǎn)i' :
case ‘eps' :
case ‘ps' :
return ‘a(chǎn)pplication/postscript';
case ‘doc' :
case ‘docx' :
return ‘a(chǎn)pplication/msword';
case ‘xls' :
case ‘xlt' :
case ‘xlm' :
case ‘xld' :
case ‘xla' :
case ‘xlc' :
case ‘xlw' :
case ‘xll' :
return ‘a(chǎn)pplication/vnd.ms-excel';
case ‘ppt' :
case ‘pps' :
return ‘a(chǎn)pplication/vnd.ms-powerpoint';
case ‘odt' :
return ‘a(chǎn)pplication/vnd.oasis.opendocument.text';
case ‘ods' :
return ‘a(chǎn)pplication/vnd.oasis.opendocument.spreadsheet';
default :
if (function_exists ( ‘mime_content_type' )) {
$fileSuffix = mime_content_type ( $filename );
} else {
$fileSuffix = ‘a(chǎn)pplication/octet-stream';
}
return $fileSuffix;
break;
}
}
相關(guān)文章
PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法
下面小編就為大家分享一篇PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03php使用yield對(duì)性能提升的測(cè)試實(shí)例分析
這篇文章主要介紹了php使用yield對(duì)性能提升的測(cè)試,結(jié)合具體實(shí)例形式對(duì)比分析了php使用yield及常規(guī)操作的運(yùn)算耗時(shí)與內(nèi)存占用情況,測(cè)試了yield對(duì)性能的提升大小,需要的朋友可以參考下2019-09-09PHP關(guān)于IE下的iframe跨域?qū)е聅ession丟失問(wèn)題解決方法
一個(gè)登錄頁(yè)面,被別的網(wǎng)站用iframe嵌進(jìn)去后,死活無(wú)法登錄(只在IE中存在這種情況)。主要是session無(wú)法被保存的問(wèn)題,下面把個(gè)人的解決過(guò)程分享個(gè)大家2013-10-10解析PHP獲取當(dāng)前網(wǎng)址及域名的實(shí)現(xiàn)代碼
本篇文章是對(duì)PHP獲取當(dāng)前網(wǎng)址及域名的代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06