Detect if Download is Complete

Posted by user604138 on Stack Overflow See other posts from Stack Overflow or by user604138
Published on 2011-02-05T07:20:01Z Indexed on 2011/02/05 7:25 UTC
Read the original article Hit count: 152

Filed under:
|
|
|
|

I have a very simple and standard PHP force download script.

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 noondownloadready event and I don't think it is possible to intercept browser events.

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?

    <?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 > $chunksize) {
           $handle = fopen($realpath, 'rb');
           $buffer = '';
           while (!feof($handle)) {
                 $buffer = fread($handle, $chunksize);
                 echo $buffer;
                 ob_flush();
                 flush();
           }
          fclose($handle);
     }             
     else {
         readfile($realpath);
     }
     exit();
     ?>

The reason I need this:

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript