Php file download function, _ PHP Tutorial

Source: Internet
Author: User
Php file download function ,. Php file download function. use the function to complete the PHP function code functiondownload ($ url, $ filename) {to obtain the file size, to prevent files larger than 2 GB, sprintf is used to read php file download functions,
PHP code for downloading objects using functions

Function download ($ url, $ filename ){
// Obtain the file size to prevent files exceeding 2 GB from being read by sprintf.
$ Filesize = sprintf ("% u", filesize ($ url ));
If (! $ Filesize ){
Return;
}
Header ("Content-type: application/octet-stream \ n"); // application/octet-stream
Header ("Content-type: unknown/unknown ;");
Header ("Content-disposition: attachment; filename = \" ". $ filename ."\"");
Header ('content-transfer-encoding: binary ');
If ($ range = getenv ('http _ range') {// when there is an offset, use the 206 breakpoint resume header.
$ Range = explode ('=', $ range );
$ Range = $ range [1];

Header ("HTTP/1.1 206 Partial Content ");
Header ("Date:". gmdate ("D, d m y h: I: s"). "GMT ");
Header ("Last-Modified:". gmdate ("D, d m y h: I: s", filemtime ($ url). "GMT ");
Header ("Accept-Ranges: bytes ");
Header ("Content-Length:". ($ filesize-$ range ));
Header ("Content-Range: bytes". $ range. ($ filesize-1). "/". $ filesize );
Header ("Connection: close". "\ n ");
} Else {
Header ("Content-Length:". $ filesize. "\ n ");
$ Range = 0;
}
LoadFile ($ url );
}

Function loadFile ($ filename, $ retbytes = true ){
$ Buffer = '';
$ Cnt = 0;
$ Handle = fopen ($ filename, 'RB ');
If ($ handle = false ){
Return false;
}
While (! Feof ($ handle )){
$ Buffer = fread ($ handle, 1024*1024 );
Echo $ buffer;
Ob_flush ();
Flush ();
If ($ retbytes ){
$ Cnt + = strlen ($ buffer );
}
}
$ Status = fclose ($ handle );
If ($ retbytes & $ status ){
Return $ cnt; // return num. bytes delivered like readfile () does.
}
Return $ status;
}
Enter two parameters to complete the download.
download($url, $filename)

Http://www.bkjia.com/PHPjc/1127369.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127369.htmlTechArticlephp download file function, through the function to complete the download file PHP function code function download ($ url, $ filename) {// get the file size, to prevent more than 2 GB of files, use sprintf to read...

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.