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!