如何从客户端向服务器发送数据

问题描述:

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?

我正在尝试编写一个服务器,它可以以分段方式下载文件。 因此,如果连接中断,下次同一客户端连接时,下载可以从停止的地方恢复。 p>

现在,假设当客户端打开网页时,服务器开始读取文件 以下列方式保存在它上面: p>

  $ handle = fopen($ file_in,“rb”); 
 $ handle_2 = fopen($ file_out,“w”)  ; 
 $ contents = fread($ handle,filesize($ file_in)); 
fwrite($ handle_2,$ contents); 
  code>  pre> 
 
 

当然,fwrite 不能使用客户端计算机上的文件。 p>

基本上我的问题是,一旦我有文件内容,如何将数据发送到客户端PC上的文件? p> div>

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.