I'm trying to write a server, which can download files in a piece-wise manner. So, if the connection is interrupted, next time the same client connects, the download can resume from where it stopped.
Now, suppose when the client opens the webpage, the server starts reading files saved on it, in the following manner:
$handle = fopen($file_in, "rb");
$handle_2 = fopen($file_out, "w");
$contents = fread($handle, filesize($file_in));
fwrite($handle_2, $contents);
Of course, the fwrite won't work with files on the client's computer.
So basically my question is, how do I send data to a file on the client's pc, once I have the file contents?
The HTTP_Download PEAR package seems to be what you are looking for:
HTTP_Download provides an interface to easily send any arbitrary data to HTTP clients. HTTP_Download can gain its data from variables, files or stream resources.
With this package you can easily handle (hidden) downloads. Hidden means not accessible by the public - for instance if you want to restrict access to particular downloads.
It supports HTTP compression, caching and partial downloads, resuming and sending raw data, for example from database BLOBs.