php断点上载

Jun 13, 2016 pm 01:07 PM
content header range size

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*/
Copy after login
?
            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']) &amp;&amp; ($_SERVER['HTTP_RANGE'] != "") &amp;&amp; preg_match("/^bytes=([0-9]+)-/i", $_SERVER['HTTP_RANGE'], $match) &amp;&amp; ($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;
Copy after login
?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use java's File.length() function to get the size of the file

What does linux header mean? What does linux header mean? Jul 18, 2023 pm 03:34 PM

What does linux header mean?

How does SpringBoot pass parameters in the Header through Feign calls? How does SpringBoot pass parameters in the Header through Feign calls? May 16, 2023 pm 08:38 PM

How does SpringBoot pass parameters in the Header through Feign calls?

What is the difference between html5 tag head and header? What is the difference between html5 tag head and header? Jan 17, 2022 am 11:10 AM

What is the difference between html5 tag head and header?

How to use PHP header() method to adjust web pages How to use PHP header() method to adjust web pages Mar 28, 2023 pm 01:54 PM

How to use PHP header() method to adjust web pages

How to jump in php header How to jump in php header Dec 02, 2022 am 09:14 AM

How to jump in php header

PHP returns all the values ​​in the array to form an array PHP returns all the values ​​in the array to form an array Mar 21, 2024 am 09:06 AM

PHP returns all the values ​​in the array to form an array

How Nginx distributes through the identity in the header How Nginx distributes through the identity in the header May 11, 2023 pm 04:01 PM

How Nginx distributes through the identity in the header

See all articles