search
HomeBackend DevelopmentPHP Tutorialmongodb - Mongo使用PHP进行断点续传的问题

1.刚开始接触mongodb,然后查询到PHP的DRIVER并不支持HTTP RANGE header,这样就无法支持断点续传,不知道该如何查询下去,不知道最近的mongoDB的php client有没有支持这个?

2.我自己想着应该可以用mongodb的chunks来实现一种断点续传,每个块默认是256K,程序根据自己下载的文件大小判断下载了多少个块,然后再请求下一块,遇到下载的chunks不是整数,删除不是整数的那一部分,然后从整数+1的那一个chunks继续下载。这种方案是否可行呢?

回复内容:

1.刚开始接触mongodb,然后查询到PHP的DRIVER并不支持HTTP RANGE header,这样就无法支持断点续传,不知道该如何查询下去,不知道最近的mongoDB的php client有没有支持这个?

2.我自己想着应该可以用mongodb的chunks来实现一种断点续传,每个块默认是256K,程序根据自己下载的文件大小判断下载了多少个块,然后再请求下一块,遇到下载的chunks不是整数,删除不是整数的那一部分,然后从整数+1的那一个chunks继续下载。这种方案是否可行呢?

关于php、关于HTTP RANGE、关于断点续传

  • 从php手册上看$_SERVER数组的信息是从Web服务器创建的信息获取,并不关php是否支持,要看你用的web服务器软件是否允许。虽然手册上并没有列出HTTP_RANGE信息。但是相关的例子不少。我看到写的较严谨的例子:http://www.thomthom.net/blog/2007/09/...
  • 你可以自己本地建立一个简易测试。比如使用它做现成测试:http://php.net/manual/zh/function.fre...
  • 下载文件,暂停再点继续。如果没有做断点续传是回到初始重新开始或者下载器报异常(你可删除相关http_range代码,验证一个事实)。事实上是php做断点续传是可行的

其实比较憋屈,文件服务器做的事让php去做了。如果是大文件,用fread一下子全部读出来,php基本会抛出异常,提示内存不够。那这里还要去控制一个读出限制。
比如我前面说那例子中是这么处理的(我简化了部分)。每次读出最多1024 * 8个字节数,到达EOF 的时候完成。

// Start buffered download
$buffer = 1024 * 8;
while(!feof($fp))
{
    set_time_limit(0); // Reset time limit for big files
    echo fread($fp, $buffer);
    flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
fclose($fp);

可以想象下,如果服务器(不管是ftp还是http等等)他们不允许断点。那些众多的下载软件基本没市场了。目的服务器支持多线程最好了,如果不允许也没关系,到其他下载点返回一个断点,继续下载,自己实现一个多断点下载。这也是下载软件的基本原理。

Gridfs Mongodb存储
你所描述的“用mongodb的chunks来实现一种断点续传”这是错误的理解。断点这不关数据库的事。你所描述的是一个读取过程,控制的一个数据输出。并且因为HTTP本身协议原因,它没能力让你“删除不是整数的部分”你只能判断出具体文件字节流,再做细致数据输出。

其实你应该往”Gridfs Mongodb存储“方向去做应用。

gridfs就是做你所描述的类似的事情。将文件分割存储到mongodb。读出时再整合。其中内部机制我没研究,但肯定比你自己实现要有效率的多。你如果有兴趣可以去研究下。针对php的mongodb驱动也是评价非常好的。使用gridfs替换原始文件存储是可行的。

并且如果你使用的是Nginx服务器软件,可以避开php读取。直接做成Nginx模块。让Nginx去读取文件,会比使用php更高效。当然了写入还是用php。

HTTP 断点上传有难度,单纯的依靠它自身实现不了。目前的方案挺多:java、flash、silverlight、activex等等做客户端控制,然后服务器端再做处理等等。目前我知道的做的比较优秀的是Youtube:https://support.google.com/youtube/bi... (需要翻一下)

从原理上说,GridFS至少应该支持按照Chunk来读写,所以断点续传是可以实现的。但是由于目前的php sdk里头没有细化到chunk这一层,所以如果需要这么做的话,可能得自己改改。

mongodb 的 chunks 写入后不支持修改,所以你要注意到如果 chunks 尺寸过大会让你在下次传递的时候丢失一部分数据,上传客户端也先要从服务器获取已经上传的 chunks 的数量乘以尺寸为续传应该开始的地方。

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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 Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools