php断点上载

WBOY
发布: 2016-06-13 13:07:16
原创
789 人浏览过

php断点下载

?

<?php /**

 * PHP-HTTP断点续传实现

 * @param string $path: 文件所在路径

 * @param string $file: 文件名

 * @return void

 */

function download($path, $file) {
	
	$real = $path . '/' . $file;
	
	if (! file_exists ( $real )) {
		
		return false;
	
	}
	
	$size = filesize ( $real );
	
	$size2 = $size - 1;
	
	$range = 0;
	
	if (isset ( $_SERVER ['HTTP_RANGE'] )) {
		
		header ( 'HTTP /1.1 206 Partial Content' );
		
		$range = str_replace ( '=', '-', $_SERVER ['HTTP_RANGE'] );
		
		$range = explode ( '-', $range );
		
		$range = trim ( $range [1] );
		
		header ( 'Content-Length:' . $size );
		
		header ( 'Content-Range: bytes ' . $range . '-' . $size2 . '/' . $size );
	
	} else {
		
		header ( 'Content-Length:' . $size );
		
		header ( 'Content-Range: bytes 0-' . $size2 . '/' . $size );
	
	}
	
	header ( 'Accenpt-Ranges: bytes' );
	
	header ( 'application/octet-stream' );
	
	header ( "Cache-control: public" );
	
	header ( "Pragma: public" );
	
	//解决在IE中下载时中文乱码问题
	

	$ua = $_SERVER ['HTTP_USER_AGENT'];
	
	if (preg_match ( '/MSIE/', $ua )) {
		
		$ie_filename = str_replace ( '+', '%20', urlencode ( $file ) );
		
		header ( 'Content-Dispositon:attachment; filename=' . $ie_filename );
	
	} else {
		
		header ( 'Content-Dispositon:attachment; filename=' . $file );
	
	}
	
	$fp = fopen ( $real, 'rb+' );
	
	fseek ( $fp, $range );
	
	while ( ! feof ( $fp ) ) {
		set_time_limit ( 0 );
		
		print (fread ( $fp, 1024 )) ;
		
		flush ();
		
		ob_flush ();
	
	}
	
	fclose ( $fp );

}
/*End of PHP*/
登录后复制
?
            ob_start();
            $size = filesize($file_dir . $file_name);
            // 输入文件标签
            Header("Content-type: application/octet-stream");
            Header("Cache-Control: must-revalidate, post-check=0, pre-check=0");            
            Header("Content-length: " . $size);
            
            $encoded_filename = urlencode($down_name);
            $encoded_filename = str_replace("+", "%20", $encoded_filename);

            $ua = $_SERVER["HTTP_USER_AGENT"];
            // 处理下载的时候的文件名
            if (preg_match("/MSIE/", $ua)) {
                header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
            } else if (preg_match("/Firefox/", $ua)) {
                header('Content-Disposition: attachment; filename*="utf8\'\'' . $down_name . '"');
            } else {
                header('Content-Disposition: attachment; filename="' . $down_name . '"');
            }
            $fp = fopen($file_dir . $file_name, "r"); // 打开文件

            if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1]  0 ? ($size - $range) : 0;
                header("Content-Length:" . $rangesize);
                header("Content-Range: bytes " . $range . '-' . ($size - 1) . "/" . $size);
                // header("Connection: close" . "\n\n" );
                //header("Connection: close"." ");
            } else {                
                header("Content-Length: " . (string) ($size));
                header("Accept-Ranges: bytes");
                $range = 0;
                header("Content-Range: bytes " . $range . '-' . ($size - 1) . "/" . $size);
            }
            fpassthru($fp);
            ob_end_flush();
            fclose($fp);
            exit;
登录后复制
?
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板