教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 断点续传的简单例子

php 断点续传的简单例子

发布时间:2016-09-12   编辑:jiaochengji.com
本文介绍下,php实现断点续传的简单例子,有需要的朋友参考下。

分享一个php 断点续传的代码,供大家参考。

代码如下:

<?php
/**
* php断点续传
* edit by www.jbxue.com
*/
$fname = './MMLDZG.mp3';  
$fp = fopen($fname,'rb');  
$fsize = filesize($fname);  
if (Isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-$/i",
 $_SERVER['HTTP_RANGE'], $match) && ($match[1] < $fsize)) {
    $start = $match[1]; 
} else {
    $start = 0; 
} 
@header("Cache-control: public"); @header("Pragma: public"); 
if ($star--> 0) {  
    fseek($fp, $start);  
    Header("HTTP/1.1 206 Partial Content");  
    Header("Content-Length: " . ($fsize - $start));  
    Header("Content-Ranges: bytes" . $start . "-" . ($fsize - 1) . "/" . $fsize);  
} else {  
    header("Content-Length: $fsize");  
    Header("Accept-Ranges: bytes");  
}  
@header("Content-Type: application/octet-stream");  
@header("Content-Disposition: attachment;filename=mmdld.mp3");  
fpassthru($fp);  
?>

代码说明:
fpassthru() 函数输出文件指针处的所有剩余数据。
该函数将给定的文件指针从当前的位置读取到 EOF,并把结果写到输出缓冲区。

这里有两篇远程文件断点续传的文章,有需要的朋友,作个参考:
php 超大文件下载 断点续传下载
php 下载远程文件 支持断点续传

您可能感兴趣的文章:
php 断点续传的简单例子
php文件上传代码大全(实例分享)
PHP 中使用 TUS 协议来实现大文件的断点续传
php 文件上传简单实例
php断点续传下载示例代码
PHP日常实用小Tips
php ftp文件上传函数的简单例子
php下载远程文件的类(支持断点续传)
php断点续传分割合并文件实例代码
断点续传原理是什么?怎么利用html5实现文件断点续传

[关闭]
~ ~