Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

php - Detect if Download is Complete

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check out this Sitepoint Forum Post that describes the solution.

Basically, once the while loop breaks, you're done!

Here's the full thread that describes using an AJAX poll to detect when the download is complete: http://www.sitepoint.com/forums/showthread.php?t=618233


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...