Download Status with PHP and JavaScript

前端 未结 2 2055
我寻月下人不归
我寻月下人不归 2021-01-15 07:12

I\'m currently looking into a way of showing the file download status on a page. I know this isnt needed since the user usually has a download status in the browser, but I w

2条回答
  •  盖世英雄少女心
    2021-01-15 07:19

    You can do it with two seperate php files, first file for downloading process. Like as:

    $strtTime=time();
    $download_rate=120;   //downloading rate  
        $fp = fopen($real, "r");
          flush();// Flush headers
        while (!feof($fp)) {   
         $downloaded=round($download_rate * 1024);
            echo fread($fp,$downloaded );
            ob_flush();
           flush(); 
            if (connection_aborted ()) {
    
         // unlink("yourtempFile.txt" ;
                exit;
            }
    
         $totalDw +=$downloaded;
         // file_put_contents("yourtempFile.txt", "downloaded: $totalDw ; StartTime:$strtTime");
              sleep(1);
        }
        fclose($fp);
       // unlink("yourtempFile.txt") ;
    

    Second file would be used for reading yourtempFile.txt by Ajax continusly. Using Sessions and Cookies wouldn't be used because of starting print.

提交回复
热议问题