PHP download file function instance code, _ PHP Tutorial

Source: Internet
Author: User
Tags php download
PHP downloads the function instance code of the file ,. Function instance code of the PHP file to be downloaded. the Function Code functiondownload ($ url, $ filename) {obtains the file size to prevent files larger than 2 GB, use spr PHP to download the function instance code of the file,

PHP code for downloading objects using functions

Function download ($ url, $ filename) {// Obtain the file size. to prevent files larger than 2 GB, use sprintf to read $ 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 resumable Upload 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 download ($ url, $ filename)

The above section describes the knowledge of function instance code for downloading PHP files. I hope it will be helpful to you. if you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Complete: use the function to download the PHP function code of the file. function download ($ url, $ filename) {// Obtain the file size to prevent files exceeding 2 GB. use spr...

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.