php 下载文件的函数,_PHP教程

WBOY
Lepaskan: 2016-07-12 08:52:26
asal
920 orang telah melayarinya

php 下载文件的函数,

通过函数完成下载文件的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个参数即可完成下载
Salin selepas log masuk
download($url, $filename)
Salin selepas log masuk

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127369.htmlTechArticlephp 下载文件的函数, 通过函数完成下载文件的PHP功能代码 function download($url, $filename) { // 获得文件大小, 防止超过2G的文件, 用sprintf来读...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan