I would like to set up a script that reads an mp3 file using the readfile function in PHP. Only problem is I do not want to wait for the mp3 file to be finished encoding before I start reading for the end user.
Assuming that you are spawning the encoder as a separate process from PHP, you should use popen
to do so and instruct it to spew the encode at stdout
. You can then easily use fread
on the pipe that represents the encoder's standard output and forward whatever you read to the browser.
Something like this:
// Assuming that you have already sent the proper
// HTTP headers to serve a download earlier
$handle = popen('encoder --param --output-to-stdout', 'r');
while (!$feof($handle)) {
echo fread($handle, 2048); // or some other size, doesn't really matter
}
pclose($handle);