Allow PHP to support source code for resumable upload

Source: Internet
Author: User
Tags ranges forum software

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 ));
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.