开发者

Streaming local file from PHP while it's been written to by a CURL process

开发者 https://www.devze.com 2022-12-15 18:54 出处:网络
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:

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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消