-2

I am looking to create a direct connection between a remote server and a client, where the data transfer is controlled by a PHP script. The script would be responsible for determining what chunks of data to transfer from the remote server to the client; I don't want the data to transfer through the server hosting the PHP script.

Essentially, I am trying to stream data from a remote server to a client, where the transferred chunks of data is controlled by a PHP script.

I took a look at PHP's stream functions (http://php.net/manual/en/book.stream.php), but this does not seem to provide a solution.

Does PHP provide a function to select a specific range of bytes from a remote file, and transfer it directly to the client?

Hope4You
  • 1,927
  • 4
  • 21
  • 45

1 Answers1

0

Your question is not fully specified, but I'll assume that you have a typical PHP deployment i.e. you are expecting a client to make http requests to your server, you have a server that interprets php code upon said requests, and there's some "remote server" that you want to make and http request to and have the client receive that response. When you say "stream data directly from the remote server" it sounds like you want the client to make a direct connection to the remote server and control that connection with PHP, this, strictly speaking is not possible, but there are ways to loosely interpret what you're saying that are possible.

If you're willing to relax the "controlled by PHP script requirement"

You can make the client create a direct connection to the remote-server by sending, from your php script, javascript that will run on the client and the client will create the direct connection. In this scenario, if the remote server supports it, you can use http content ranges to request the specific range of bytes. E.g. xhr.setRequestHeader('content-range', 'bytes 0-1233/1234'); See:

In this scenario, you're not controlling the content ranges with php. You're using PHP to send javascript that requests the content ranges.

Community
  • 1
  • 1
Drew Faubion
  • 441
  • 3
  • 12
  • 1
    I'm guessing that Hope4you wants to send a complete file not just a chunk. How would the assembly happen by sending multiple xhr headers? I guess the JavaScript would be in charge of orchestrating this in the browser and then assemble it to a file before letting the user have it? – Michael Mar 01 '16 at 02:51
  • I give an example of requesting a chunk because he asked "Does PHP provide a function to select a specific range of bytes from a remote file, and transfer it directly to the client?" Using content-range's are the only way I know to make a client request a specific range of bytes. – Drew Faubion Mar 01 '16 at 02:53
  • Yeah I got that, was just thinking about how the assembly could happen. Great answer, I didn't know about content-range. Is it widely enabled by servers, do you know? (Apache, Nginx) – Michael Mar 01 '16 at 02:56