php 下载文件的函数 - carlo-jie
通过函数完成下载文件的PHP功能代码<br><br>function download($url, $filename) {<br> // 获得文件大小, 防止超过2G的文件, 用sprintf来读<br> $filesize = sprintf ( "%u", filesize ( $url ) );<br> if (! $filesize) {<br> return;<br> }<br> header ( "Content-type:application/octet-stream\n" ); //application/octet-stream<br> header ( "Content-type:unknown/unknown;" );<br> header ( "Content-disposition: attachment; filename=\"" . $filename . "\"" );<br> header ( 'Content-transfer-encoding: binary' );<br> if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候,采用206的断点续传头<br> $range = explode ( '=', $range );<br> $range = $range [1];<br><br> header ( "HTTP/1.1 206 Partial Content" );<br> header ( "Date: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );<br> header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", filemtime ( $url ) ) . " GMT" );<br> header ( "Accept-Ranges: bytes" );<br> header ( "Content-Length:" . ($filesize - $range) );<br> header ( "Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize );<br> header ( "Connection: close" . "\n\n" );<br> } else {<br> header ( "Content-Length:" . $filesize . "\n\n" );<br> $range = 0;<br> }<br> loadFile ( $url );<br>}<br><br>function loadFile($filename, $retbytes = true) {<br> $buffer = '';<br> $cnt = 0;<br> $handle = fopen ( $filename, 'rb' );<br> if ($handle === false) {<br> return false;<br> }<br> while ( ! feof ( $handle ) ) {<br> $buffer = fread ( $handle, 1024 * 1024 );<br> echo $buffer;<br> ob_flush ();<br> flush ();<br> if ($retbytes) {<br> $cnt += strlen ( $buffer );<br> }<br> }<br> $status = fclose ( $handle );<br> if ($retbytes && $status) {<br> return $cnt; // return num. bytes delivered like readfile() does.<br> }<br> return $status;<br>}<br>输入2个参数即可完成下载
Copy after login
download($url, $filename)
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD
Nordhold: Fusion System, Explained
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
Java Tutorial
1664
14


CakePHP Tutorial
1423
52


Laravel Tutorial
1321
25


PHP Tutorial
1269
29


C# Tutorial
1249
24

