当 CURL 进程写入本地文件时,从 PHP 流式传输本地文件

发布于 2024-08-17 04:31:06 字数 785 浏览 6 评论 0原文

我正在为我的网站创建一个简单的代理服务器。为什么我不使用 mod_proxy 和 mod_cache 是一个不同的讨论。代码如下:

    shell_exec("nohup curl --create-dirs -o {$write_path} {$source_url} > /dev/null 2> /dev/null & echo $!");
    sleep(1);

    $read_speed = 65.5; # 65.5 kb/s download rate
    $handle = fopen($write_path, "rb");

    $content_type = select_meta_item($headers, 'Content-Type');
    $file_size = select_meta_item($headers, 'Content-Length');
    send_headers($content_type, $file_size); 
    flush();

    while (!feof($handle))
    {
        echo fread($handle, round($read_speed * 1024));
        flush();
        sleep(1);
    }

    fclose($handle);

使用此方法无法传输 MP3。可在 Chrome 中播放,但不能在 Firefox 中播放。最初我将使用它通过 Long Tail 的 JW Player 流式传输 MP3 文件。如果一切顺利,我还将使用它来发送 ZIP 文件。

I am creating a simple Proxy server for my website. Why I am not using mod_proxy and mod_cache is a different discussion. Here's the code:

    shell_exec("nohup curl --create-dirs -o {$write_path} {$source_url} > /dev/null 2> /dev/null & echo $!");
    sleep(1);

    $read_speed = 65.5; # 65.5 kb/s download rate
    $handle = fopen($write_path, "rb");

    $content_type = select_meta_item($headers, 'Content-Type');
    $file_size = select_meta_item($headers, 'Content-Length');
    send_headers($content_type, $file_size); 
    flush();

    while (!feof($handle))
    {
        echo fread($handle, round($read_speed * 1024));
        flush();
        sleep(1);
    }

    fclose($handle);

Streaming an MP3 doesn't work using this method. Plays in Chrome, but not in Firefox. Initially I'll be using this to stream MP3 files through Long Tail's JW Player. If it all works out, I'll also be using this to send ZIP files.

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

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

发布评论

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

评论(2

甜味超标? 2024-08-24 04:31:06

问题是您的文件格式是否可以由客户端实现进行流式传输。如果 Firefox 不支持播放 mp3,直到完成下载后,再多的服务器端欺骗也无法帮助您实现流式传输。您将需要客户端支持,例如基于 Flash 的内联播放器。

对于 ZIP 文件,由于编码表位于存档的末尾,因此在完全下载之前,任何软件都无法打开它。

顺便说一句,您是否考虑过创建一个 FIFO,将 curl 指向 FIFO 输入,并将 readfile 应用于 FIFO 输出,从而让操作系统处理事务?

The question is whether your file format can be streamed by the client implementation. If Firefox does not support playing an mp3 until it is done being downloaded, no amount of server-side trickery will help you achieve streaming. You will need client-side support, such as a flash-based inline player.

For ZIP files, given that the encoding table is placed at the end of the archive, no software will be able to open it until it has been completely downloaded.

As a side note, have you considered creating a FIFO, pointing curl at the FIFO input, and applying readfile to the FIFO output, thereby letting the OS handle things?

隔纱相望 2024-08-24 04:31:06

我想通了。代码工作正常。

APACHE 正在通过 404 处理程序 (ErrorDocument) 调用该文件。 Apache 在调用 PHP 脚本之前自动发送 404 标头。

该文件(上面的代码)不会启动 CURL 进程并重定向到流式传输的文件。由于 Apache 返回了 404,Firefox 忽略了 MP3 响应。 (而 Chrome 没有)。现在我重定向了,效果很好。

I figured it out. The code it works fine.

The file was being called by APACHE by the 404 handler (ErrorDocument). Apache automatically sent the 404 header prior to the PHP script being called.

This file (code above), not starts the CURL process and redirects to a file that streams. Since Apache returned the 404, Firefox ignored the MP3 response. (whereas Chrome didn't). Now that I redirect, it works fine.

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