下载:

原理:

Http头的 Range、Content-Range()
未启用单点登录
Gerenal
Request URL: http://www.demotest.com/php/fileDownload/demo.php
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:80
Referrer Policy: no-referrer-when-downgrade
response响应头
HTTP/1.1 200 OK
Date: Thu, 02 May 2019 11:03:40 GMT
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/7.2.14
X-Powered-By: PHP/7.2.14
cache-control: public
content-disposition: attachment; filename=1556795020.zip
Content-Length: 452368104
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/octet-stream
Request请求头
GET /php/fileDownload/demo.php HTTP/1.1
Host: www.demotest.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
启用单点登录
<?php
/** php下载类,支持断点续传
* Date: 2013-06-30
* Author: fdipzone
* Ver: 1.0
*
* Func:
* public download: 下载文件
* public setSpeed: 设置下载速度
* private getRange: 获取header中Range
*/ class FileDownload{ // class start private $_speed = 512; // 下载速度 /** 下载
* @param String $file 要下载的文件路径
* @param String $name 文件名称,为空则与下载的文件名称一样
* @param boolean $reload 是否开启断点续传
*/
public function download($file, $name='', $reload=false){
if(file_exists($file)){
if($name==''){
$name = basename($file);
} $fp = fopen($file, 'rb');
$file_size = filesize($file);
$ranges = $this->getRange($file_size); header('cache-control:public');
header('content-type:application/octet-stream');
header('content-disposition:attachment; filename='.$name); if($reload && $ranges!=null){ // 使用续传
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges:bytes'); // 剩余长度
header(sprintf('content-length:%u',$ranges['end']-$ranges['start'])); // range信息
header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size)); // fp指针跳到断点位置
fseek($fp, sprintf('%u', $ranges['start']));
}else{
header('HTTP/1.1 200 OK');
header('content-length:'.$file_size);
} while(!feof($fp)){
echo fread($fp, round($this->_speed*1024,0));
ob_flush();
//sleep(1); // 用于测试,减慢下载速度
} ($fp!=null) && fclose($fp); }else{
return '';
}
} /** 设置下载速度
* @param int $speed
*/
public function setSpeed($speed){
if(is_numeric($speed) && $speed>16 && $speed<4096){
$this->_speed = $speed;
}
} /** 获取header range信息
* @param int $file_size 文件大小
* @return Array
*/
private function getRange($file_size){
if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
$range = $_SERVER['HTTP_RANGE'];
$range = preg_replace('/[\s|,].*/', '', $range);
$range = explode('-', substr($range, 6));
if(count($range)<2){
$range[1] = $file_size;
}
$range = array_combine(array('start','end'), $range);
if(empty($range['start'])){
$range['start'] = 0;
}
if(empty($range['end'])){
$range['end'] = $file_size;
}
return $range;
}
return null;
} } // class end ?>
  1. Request URL:
    http://www.demotest.com/php/fileDownload/demo.php
  2. Request Method:
    GET
  3. Status Code:
    200 OK
  4. Remote Address:
    127.0.0.1:80
  5. Referrer Policy:
    no-referrer-when-downgrade

php断点续传之文件上传与文件下载的更多相关文章

  1. struts2的文件上传和文件下载

    实现使用Struts2文件上传和文件下载: 注意点: (1)对应表单的file1和私有成员变量的名称必须一致 <input type="file" name="fi ...

  2. 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载

    摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...

  3. Struts2文件上传和文件下载

    一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...

  4. Struts2 文件上传和文件下载

    一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...

  5. struts文件上传、文件下载

    文件上传 如果在表单中上传文件,表单的enctype属性为multipart/form-data struts默认上传文件大小为2M,如果需要修改,在配置文件中设置 <constant name ...

  6. 支持断点续传的文件上传插件——Huploadify-V2.0来了

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  7. struts2实现文件上传、多文件上传和文件下载

    总结的两个问题,就是struts2上传下载的时候对属性名配置要求非常严格: 第一:上传的时候 private File file; private String fileContentType; pr ...

  8. struts2 笔记02 文件上传、文件下载、类型转换器、国际化的支持

    Struts2的上传 1. Struts2默认采用了apache commons-fileupload  2. Struts2支持三种类型的上传组件 3. 需要引入commons-fileupload ...

  9. struts2中的文件上传,文件下载

    文件上传: Servlet中的文件上传回顾 前台页面 1.提交方式post 2.表单类型 multipart/form-data 3.input type=file 表单输入项 后台 apache提交 ...

随机推荐

  1. php面向对象深入理解(二)

    一个简单的小程序:   配置 config.ini <?php //项目的根目录 define("ROOT","F:/文件夹的名字/oop/"); //数 ...

  2. 【Codeforces Round #429 (Div. 2) B】 Godsend

    [Link]:http://codeforces.com/contest/841/problem/B [Description] 两个人轮流对一个数组玩游戏,第一个人可以把连续的一段为奇数的拿走,第二 ...

  3. 第k小团+bitset优化——牛客多校第2场D

    模拟bfs,以空团为起点,用堆维护当前最小的团,然后进行加点更新 在加入新点时要注意判重,并且用bitset来加速判断和转移构造 #include<bits/stdc++.h> #incl ...

  4. NX二次开发-UFUN查询对象的类型和子类型UF_OBJ_ask_type_and_subtype

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  5. Java-javaFx库运用-时钟显示

    JavaFx是开发Java GUI程序的新框架.JavaFX应用可以无缝地在桌面或web浏览器中运行.具有内建的2D.3D动画支持,以及视频和音频的回放功能,可以作为一个应用独立运行或者在浏览器中运行 ...

  6. ionic-CSS:ionic 单选框

    ylbtech-ionic-CSS:ionic 单选框 1.返回顶部 1. ionic 单选框 ionic 单选按钮与标准 type="radio" 的 input元素类似.以下展 ...

  7. The packaging for this project did not assign a file to the build artifact

    当进行mvn install时,遇到以下错误 The packaging for this project did not assign a file to the build artifact 在网 ...

  8. class5_Radiobutton 选择按钮(选项选择)

    最终的运行效果图(程序见序号4) #!/usr/bin/env python# -*- coding:utf-8 -*-# -------------------------------------- ...

  9. Java父类强制转换子类原则

    最近,微信群友在讨论子类父类的转换问题,其实不难,给大家用实例来说明一下就很明了了. 我们知道Java中子类转换成父类是没有任何问题的,那父类可以转换成子类吗? 来看下面这段程序: public cl ...

  10. 收藏的链接-English

    What is the adverb for deposit? https://www.wordhippo.com/what-is/the-adverb-for/deposit.html