如何使用 PHP 传输媒体文件?

发布于 2024-09-24 21:33:08 字数 575 浏览 0 评论 0原文

我正在尝试构建一个应用程序,在其中我必须将媒体文件(音频和视频)流式传输到浏览器。我正在通过 php 读取文件并将数据发送到浏览器。我正在使用以下代码。

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Type: {$file->getMimetype()}");
header("Content-Disposition: inline; filename=".$filename.";");
header("Content-Length: ".strlen($file_content));

echo $file_content;

一切工作正常,除了当我尝试转发视频或音频时(我的意思是假设当前播放位置是 0:15,它直接转到 1:25),媒体停止,当我再次按下播放按钮时,它从头开始。

我认为问题出在缓冲上,但无法弄清楚。我在标头中做错了什么吗?还是需要其他东西。

谢谢。

I am trying to build an application in which i have to stream the media files (audio and video) to the browser. I am reading the file through php and send the data to browser. I am using the following code.

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Type: {$file->getMimetype()}");
header("Content-Disposition: inline; filename=".$filename.";");
header("Content-Length: ".strlen($file_content));

echo $file_content;

Every thing is working fine, except when i try to forward the video or audio, (I mean suppose current play location is 0:15 and it directly go to 1:25), media stops and when i press the play button again, it starts from the beginning.

I think the problem is with the buffering, but can't figure it out. Am i doing something wrong in header or something else is required.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金兰素衣 2024-10-01 21:33:08

我认为您需要实现 Range 标头,以便客户端可以跳到文件中的特定位置。您可能可以通过 嗅探玩家发送的请求

I think you need to implement the Range header, so that the client can skip to a specific position in the file. You can probably find out what goes wrong by sniffing the request the player sends.

一生独一 2024-10-01 21:33:08

你想要的叫做“Content-Range requests”,

看看这里 使用PHP发送文件时可以断点续传吗?

What you want is called "Content-Range requests"

Have a look here Resumable downloads when using PHP to send the file?

旧街凉风 2024-10-01 21:33:08

我最近遇到这个可能对您有帮助:

http://www. jasny.net/articles/how-i-php-x-sendfile/

您可以使用 x-sendfile,而不是通过 PHP 传递整个文件(这会占用内存)。这是一个 Apache 模块,允许您运行 PHP 程序,但一旦您的代码完成了需要执行的操作(身份验证等),就将控制权传回 Web 服务器来处理实际的文件下载。

这意味着您的 PHP 代码不必担心文件的服务方式;让网络服务器执行其设计目的。

希望有帮助。

I came across this recently which may help you:

http://www.jasny.net/articles/how-i-php-x-sendfile/

Rather than passing the whole file through PHP (which eats up memory), you can use x-sendfile. This is an Apache module which allows you to run a PHP program, but pass control back to the web server to handle the actual file download once your code has done what it needs to do (authentication, etc).

It means that your PHP code doesn't have to worry about how the file is served; let the web server do what it's designed for.

Hope that helps.

屋顶上的小猫咪 2024-10-01 21:33:08

这是一个很好的教程,您只需要 PHP 部分,但仍然:
http://www.devshed.com/ c/a/PHP/Video-Streaming-PHP-Script-Tutorial/3/

Here is a good tutorial for it, you only want the PHP section but still:
http://www.devshed.com/c/a/PHP/Video-Streaming-PHP-Script-Tutorial/3/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文