PHP download file function instance code _ php instance

Source: Internet
Author: User
Tags php download
This article introduces the function-related knowledge of php File Download through the instance code. The introduction is very detailed and has reference value. If you are interested, learn it together. 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. I would like to thank you for your support for the script home website!

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.