PHP download large files

There is a project about 700M large files need to be downloaded to the local computer and the Internet to find a piece of code, as follows:

 1 <?php
 2             $realname = "xxx.pdf";
 3             $filename = "xxx.pdf";
 4             set_time_limit(0);
 5             $filesize = filesize($realname);
 6             header('Content-Description: File Transfer');
 7             header('Content-Type: application/octet-stream');
 8             header('Content-Transfer-Encoding: binary');
 9             header('Accept-Ranges: bytes');
10             header('Expires: 0');
 . 11              header ( 'the Cache-Control: MUST-revalidate' );
 12 is              header ( 'Pragma: public' );
 13 is              header ( 'the Content-the Length:'. $ Filesize );
 14              header ( 'the Content-Disposition: Attachment; = filename '. $ filename );
 15  
16              // open file 
. 17              $ FP = the fopen ( $ RealName of ,' RB ' );
 18 is              // set the position of the pointer 
. 19              fseek ( $ FP , 0 );
 20 is  
21 is              // open buffer 
twenty two             ob_start ();
 23 is              // fetch the file 
24              the while (! feof ( $ FP )) {
 25                  $ chunk_size = 1024 *. 8; // 8KB 
26 is                  echo  fread ( $ FP , $ chunk_size );
 27                  ob_flush (); // refresh buffer PHP to the Web server 
28                  the flush (); // refresh buffer Web server to the browser 
29                  SLEEP (. 1); // every one second to download KB. 8 
30              }
 31 is              // Close buffer 
32              ob_end_clean();
33 
34             fclose($fp);       

! Download perfect but when deployed to the line, there is a problem:

Description: Test environment for http, https online environment

Continue google ...., find the following code:

<?php
            $realname = "xxx.pdf";
            $filename = "xxx.pdf";
            set_time_limit(0);
            $filesize = filesize($realname);
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Transfer-Encoding: binary');
            header('Accept-Ranges: bytes');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . $filesize);
            header('Content-Disposition: attachment; filename=' . $filename);

            // 打开文件
            $fp = fopen($realname, 'rb');
            ob_clean();
            ob_end_flush();
            while (!feof($fp)) {
                echo fread($fp, $ Filesize );
                 ob_flush (); // refresh buffer to the Web server PHP 
                flush (); // refresh the Web server to the browser buffer 
            } 

            fclose ( $ fp );
             Exit ;

The perfect solution!

Guess you like

Origin www.cnblogs.com/weibofang/p/10957016.html