Detect if download is complete

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

How to check if the download is completed to notify the user on the client side? I don’t even need to show real-time progress, I’m only interested in a specific event: “when the download is complete”. Based on my research, it looks like it would have to be determined using serveride, since there is no event ondownloadready, and I don't think it is possible to intercept browser events.

So, it seems to me that it would be best to compare the bytes assigned to the common bytes with some kind of clientide / severside interaction. How can I check the bytes sent from the server to force PHP to load? Is there some kind of global PHP variable that stores this data that I can execute using 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:

The project I'm working on requires that, after the download starts, the page redirects (or displays) the "Wait while the download is complete" page. Then, once it is complete, it should redirect (or display) the page "Your download is completed, thank you." I am open to other ideas that achieve the same result.

+3
source share

Source: https://habr.com/ru/post/1790149/


All Articles