在 PHP 中流式传输远程文件会将其保存到我的服务器。为什么?

Streaming a remote file in PHP is saving it to my server. Why?

我只想流式传输文件,不想将其保存在我的内存或硬盘上。我说的是为每个观众单独播放高清视频。

有我的代码:

// ...
ob_start();
header('...'); // I am sending some headers here
ob_flush(); flush(); ob_clean();

$handle = fopen('http://example.com/bigMovie.mp4', 'rb');
ob_flush(); flush(); ob_clean();
while(!feof($handle)) // do this until is the end of the file
{
    echo fread($handle, 102400); // reading 100 kb from file
    ob_flush(); flush(); ob_clean(); // sending it to the user, cleaning ram
}
ob_end_flush(); // finished sending the file
fclose($handle); // closing the remote connection to example.com
// ...

当我尝试将 bigMovie.mp4 发送给观众时,我服务器的硬盘已满。

我该怎么办?我的代码有什么问题吗? 谢谢

这应该有效:

# Send headers
header('...');
header('...');
header('...');
...

# Send file
readfile('http://example.com/bigMovie.mp4');

readfile() 读取文件(甚至通过 http 远程读取)并立即输出其内容。


顺便说一句,您对 ob_* 函数的使用完全没有意义。在使用它之前,您需要了解 php 中输出控制的工作原理。手册是一个很好的起点:http://php.net/manual/en/ref.outcontrol.php

@hek2mgl

假设这些是您流式传输给用户的文件。您可以将文件放在 CDN 中。根据您的托管偏好,这里有几个大玩家。

您可以使用 Amazon Web Service 的 Cloudfront 进行流式传输。 http://aws.amazon.com/cloudfront/streaming/

RackSpace CDN 流媒体 http://www.rackspace.com/knowledge_center/frequently-asked-question/getting-started-with-cloud-files-streaming

Azure CDN http://blog.thoughtstuff.co.uk/2014/01/streaming-mp4-video-files-in-azure-storage-containers-blob-storage/