文档详情

php支持分块与断点续传文件功能代码.doc

发布:2017-05-18约4.84千字共7页下载文档
文本预览下载声明
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 + $st
显示全部
相似文档