For example, the first request for a file is from 0 to 999 bytes, the second request is from 1000 to 1999 bytes, and so on, each request is 1000 bytes of content, then, the program uses the fseek function to obtain the corresponding file location and output the file.
Copy codeThe Code is as follows:
$ Fname = './05e58c19552bb26b158f6621a6650899 ';
$ Fp = fopen ($ fname, 'rb ');
$ Fsize = filesize ($ fname );
If (isset ($ _ SERVER ['HTTP _ range']) & ($ _ SERVER ['HTTP _ range']! = "") & Preg_match ("/^ bytes = ([0-9] +)-$/I", $ _ SERVER ['HTTP _ range'], $ match) & ($ match [1] <$ fsize )){
$ Start = $ match [1];
} Else {
$ Start = 0;
}
@ Header ("Cache-control: public ");
@ Header ("Pragma: public ");
If ($ start> 0 ){
Fseek ($ fp, $ start );
Header ("HTTP/1.1 206 Partial Content ");
Header ("Content-Length:". ($ fsize-$ start ));
Header ("Content-Ranges: bytes". $ start. "-". ($ fsize-1). "/". $ fsize );
} Else {
Header ("Content-Length: $ fsize ");
Header ("Accept-Ranges: bytes ");
}
@ Header ("Content-Type: application/octet-stream ");
@ Header ("Content-Disposition: attachment; filename = 1.rm ");
Fpassthru ($ fp );
You can also take a look at Discuz! The attachment. php file of the forum software enables resumable data transfer. See the Code:
The RANGE of the file requested by the user is also obtained through $ _ SERVER ['HTTP _ range']. For details, see the source code analysis. Here, I will start to talk about it.
Copy codeThe Code is as follows:
$ Range = 0;
If ($ readmod = 4 ){
Dheader ('Accept-Ranges: bytes ');
If (! Emptyempty ($ _ SERVER ['HTTP _ range']) {
List ($ range) = explode ('-', (str_replace ('bytes = ', '', $ _ SERVER ['HTTP _ range']);
$ Rangesize = ($ filesize-$ range)> 0? ($ Filesize-$ range): 0;
Dheader ('content-Length: '. $ rangesize );
Dheader ('HTTP/1.1 206 Partial content ');
Dheader ('content-Range: bytes = '. $ range.'-'. ($ filesize-1).'/'. ($ filesize ));
}
}