search
php断点上载 Jun 13, 2016 pm 01:07 PM
contentheaderrangesize

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;
?
Statement
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
使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

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

linux的header是指在文件或数据流中的开头部分,用于包含关于内容的元数据,通过正确地编写和使用Header文件,开发者能够更好地利用系统资源,提高代码的可读性和可维护性。

SpringBoot怎么通过Feign调用传递Header中参数SpringBoot怎么通过Feign调用传递Header中参数May 16, 2023 pm 08:38 PM

【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中使用Feign请求另一个服务的Api接口时,有将Header中参数传递下去的需求,如果不做特殊处理,就会将Header中的参数丢失。解决方案方案一:通过@RequestHeader(name="headerName")来传递例如:Feign定义如下@FeignClient(name="service-name")pub

html5标签head和header有什么区别html5标签head和header有什么区别Jan 17, 2022 am 11:10 AM

区别:1、head标签用于定义文档头部,它是所有头部元素的容器,而header标签用于定义文档的页眉(介绍信息);2、浏览器都支持head标签,而旧版本浏览器均不支持header标签,需要IE9+以上浏览器才支持header标签。

如何使用PHP header()方法来调整网页如何使用PHP header()方法来调整网页Mar 28, 2023 pm 01:54 PM

PHP是一种功能强大的编程语言,可以用来创建动态网站和Web应用程序。其中一个最强大的功能之一是PHP的header()方法。在本文中,我们将探讨如何使用PHP的header()方法来调整网页。

php header头怎么实现跳转php header头怎么实现跳转Dec 02, 2022 am 09:14 AM

php header实现跳转的方法:1、使用“Header("Location:$url");”语法实现跳转;2、使用if判断式实现跳转,其跳转语句如“if($_COOKIE["u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');”。

PHP返回数组所有值,组成一个数组PHP返回数组所有值,组成一个数组Mar 21, 2024 am 09:06 AM

这篇文章将为大家详细讲解有关PHP返回数组所有值,组成一个数组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。使用array_values()函数array_values()函数返回一个数组中所有值的数组。它不会保留原始数组的键。$array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values将是["bar","qux"]使用循环可以使用循环手动获取数组的所有值并将其添加到一个新

Nginx如何通过header中的标识进行分发Nginx如何通过header中的标识进行分发May 11, 2023 pm 04:01 PM

Nginx可以根据请求头中自定义的标识将请求分发到不同的服务器。具体来说,可以使用map指令将请求头中的自定义标识映射为不同的后端服务器地址,然后使用proxy_pass指令将请求转发到对应的后端服务器。以下是一个示例配置文件:http{map$http_my_header$backend{defaultbackend1.example.com;value1backend2.example.com;value2backend3.example.com;}upstreambackend1{serv

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software