Client cancels download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Client cancels download

Post by Galahad »

I'm working on a download management system. Read this thread for a description of what I'm doing. I have a problem when the user starts a download, and then cancels it. The server stops sending data to the client (which is good) but it continues to read through the file. Because it is no longer slowed down by the network transmition, the drive goes crazy until it has finished reading though. When it is a several hundred megabyte data file, that takes a while.

I open the file with:

Code: Select all

$fp = fopen($path.$dataї'realname'], "r");
and then read it with:

Code: Select all

// Read the actual file, 4k at a time
  while (!feof($fp)) {
    if (connection_status() != 0) {
    //    if (connection_aborted()) {
      // Download cancelled
      break;
    }
    $buffer = fread($fp, 4096);
    print $buffer;
  }
  fclose($fp);
Again, read my other thread for a description of what is going on in terms of getting the file to the user. I have tried both the connection_aborted and connection_status approaches. I tried register_shutdown_function, but the shutdown function is not called until after the server finishes going through the file. That confirms that the script is not exiting when the user hits cancel.

I did some packet capturing and found that when a page is loaded normally, the server sends a "fin" when it is done. If the user presses stop in his/her browser before the page has finished loading, the client sends a "fin" and php stops. However, when a user cancels a download, the client only sends a bunch of packets with the "rst" flag set. I believe that as a result, php is not seeing the connection as aborted and continues to read the file even though apache has stopped sending the data to the client. Does php just not get notified that the client has tried to reset the connection and it should stop working on a page for the client? It seems like it ought to stop in that case.

I hope this is clear, if I need to clearify something let me know. I'm also not real solid in my tcp connection protocol, so I may be way off in what I think should happen with a rst. I would really appreciate some help with getting this cleared up.
Post Reply