首頁 後端開發 php教程 php断点上载

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*/
登入後複製
?
            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;
登入後複製
?
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用java的File.length()函數取得檔案的大小 使用java的File.length()函數取得檔案的大小 Jul 24, 2023 am 08:36 AM

使用java的File.length()函數取得檔案的大小

linux的header是什麼意思 linux的header是什麼意思 Jul 18, 2023 pm 03:34 PM

linux的header是什麼意思

SpringBoot怎麼透過Feign呼叫傳遞Header中參數 SpringBoot怎麼透過Feign呼叫傳遞Header中參數 May 16, 2023 pm 08:38 PM

SpringBoot怎麼透過Feign呼叫傳遞Header中參數

html5標籤head和header有什麼差別 html5標籤head和header有什麼差別 Jan 17, 2022 am 11:10 AM

html5標籤head和header有什麼差別

如何使用PHP header()方法調整網頁 如何使用PHP header()方法調整網頁 Mar 28, 2023 pm 01:54 PM

如何使用PHP header()方法調整網頁

php header頭怎麼實現跳轉 php header頭怎麼實現跳轉 Dec 02, 2022 am 09:14 AM

php header頭怎麼實現跳轉

PHP傳回數組所有值,組成一個數組 PHP傳回數組所有值,組成一個數組 Mar 21, 2024 am 09:06 AM

PHP傳回數組所有值,組成一個數組

Nginx如何透過header中的標誌進行分發 Nginx如何透過header中的標誌進行分發 May 11, 2023 pm 04:01 PM

Nginx如何透過header中的標誌進行分發

See all articles