PHP断点续传的原理与实现
断点续传主要是HTTP协议中的Content-Range报头。其理解如下:
Content-Range:响应资源的范围。可以在多次请求中标记请求的资源范围,在连接断开重新连接时,客户端只请求该资源未被下载的部分,而不是重新请求整个资源,实现了断点续传。迅雷就是基于这个原理,使用多线程分段读取网络上的资源,最后合并。关于PHP使用多线程实现断点续传稍后讨论。本文只实现简单的断点续传。
代码实现:
先定义一个函数 getRange() 这个函数用来处理 header中 Range 具体数据的处理
<span style="font-family:FangSong_GB2312;font-size:14px;">/** $file_size 文件大小 */ functiongetRange($file_size){ $range =isset($_SERVER['HTTP_RANGE'])?$_SERVER['HTTP_RANGE']:null; if(!empty($range)){ $range =preg_replace('/[\s|,].*/', '', $range); $range =explode('-',substr($range,6)); if(count($range) </span>
假设文件的地址为 $file_path
<span style="font-family:FangSong_GB2312;font-size:14px;">$speed = 512;//此参数为下载最大速度 $pos =strrpos($file_path, "/"); $file_name =substr($file_path, $pos+1); $file_size =filesize($file_path); $ranges =getRange($file_size); $fh = fopen($file_path, "rb"); header('Cache-control: public'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$file_name); if ($ranges !=null) { header('HTTP/1.1 206 Partial Content'); header('Accept-Ranges: bytes'); header(sprintf('Content-Length: %u',$ranges['end'] - $ranges['start'])); header(sprintf('Content-Range: bytes %s-%s/%s', $ranges['start'],$ranges['end'], $file_size)); fseek($fh,sprintf('%u',$ranges['start'])); }else{ header("HTTP/1.1 200 OK"); header(sprintf('Content-Length:%s', $file_size)); } while(!feof($fh)) { echo fread($fh, round($speed*1024, 0)); ob_flush(); sleep(1); } ($fh != null)&& fclose($fh); </span>
文章参考自:http://www.cnblogs.com/xyxiong/archive/2011/02/16/1956167.html

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Alipay PHP...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

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

Dreamweaver Mac version
Visual web development tools