title: Detect if Download is Complete
body: <p>I have a very simple and standard PHP force download script. </p>
  
  <p>How do I check if/when the download has completed in order to notify the user on the clientside? I don't even need to show the progress in real time, I am only interested in the very specific event: "when the download completes".  Based on my research, it seems like it would have to be determined from the serverside as there is no<code>ondownloadready</code> event and I don't think it is possible to intercept browser events.</p>
  
  <p>So it seems that my best bet would be to compare bytes sent to total bytes with some sort of clientside/severside interaction. How would I go about checking the bytes sent from the server for a PHP forced download? is there some sort of global PHP variable that store these data that I can ping with AJAX?</p>
  
  <pre><code>    &lt;?php
  
      header("Content-Type: video/x-msvideo");
      header("Content-Disposition: attachment; filename=\"".basename($realpath)."\";");
  
      ...
  
      $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
      if ($size &gt; $chunksize) {
             $handle = fopen($realpath, 'rb');
             $buffer = '';
             while (!feof($handle)) {
                   $buffer = fread($handle, $chunksize);
                   echo $buffer;
                   ob_flush();
                   flush();
             }
            fclose($handle);
       }             
       else {
           readfile($realpath);
       }
       exit();
       ?&gt;
  </code></pre>
  
  <p>The reason I need this:</p>
  
  <p>For the project I am working on, it is required that after the download starts, the page redirects to (or displays) a "please wait while the download completes" page. Then, once it is complete, it should redirect to (or display) a "Your download is complete, thank you" page. I am open to other ideas that would achieve the same result.</p>