I have a PHP-server that serves audio-files by streaming them from an FTP-server not publicly available.
After sending the approriate headers, I just stream the file to the client using ftp_get like this:
ftp_get($conn, 'php://output', $file, FTP_BINARY);
For reasons that has to do with Range
headers, I must now offer to only send a part of this stream:
$start = 300; // First byte to stream
$stop = 499; // Last byte to stream (the Content-Length is then $stop-$start+1)
I can do it by downloading the entire content temporarily to a file/memory, then send the desired part to the output. But since the files are large, that solution will cause a delay for the client who has to wait for the file to first be downloaded to the PHP-server before it even starts to download to the client.
Question:
How can I start streaming to php://output
from an FTP-server as soon as the first $start
bytes have been discarded and stop streaming when I've reached the '$stop' byte?
ftp_nb_get()
, it's last parameter$resumepos
andftp_nb_continue()
might perhaps be of use. – user555while ($ret == FTP_MOREDATA)
loop maybe you could count the received bytes somehow, however the PHP docs don't mention how many bytes are downloaded on everyftp_nb_continue()
. If I had to guess by looking at the FTP extension source code I would say thatftp_nb_continue()
downloads as many bytes asFTP_BUFSIZE
is set to, default being 4096. For full control I think it would be best to role your own FTP client in PHP, usingREST
and count the bytes received. – user555