您好,欢迎访问一九零五行业门户网

分享一个超好用的php header下载函数_PHP教程

复制代码 代码如下:
= $lastmodified)
    {
        header(http/1.1 304 not modified);
        return true;
    }
    if (isset($_server['http_if_unmodified_since']) && strtotime($_server['http_if_unmodified_since'])     {
        header(http/1.1 304 not modified);
        return true;
    }
    if (isset($_server['http_if_none_match']) &&  $_server['http_if_none_match'] == $etag)
    {
        header(http/1.1 304 not modified);
        return true;
    }
    if ($fancyname == '')
    {
        $fancyname = basename($filename);
    }
if ($contenttype == '')
    {
        $contenttype = 'application/octet-stream';
    }
    $filesize = $filestat['size'];
$contentlength = $filesize;
    $ispartial = false;
    if (isset($_server['http_range']))
    {
        if (preg_match('/^bytes=(d*)-(d*)$/', $_server['http_range'], $matches))
        {   
            $startpos = $matches[1];
            $endpos = $matches[2];
            if ($startpos == '' && $endpos == '')
            {
                return false;
            }
if ($startpos == '')
            {
                $startpos = $filesize - $endpos;
                $endpos = $filesize - 1;
            }
            else if ($endpos == '')
            {
                $endpos = $filesize - 1;
            }
            $startpos = $startpos             $endpos = $endpos > $filesize - 1 ? $filesize - 1 : $endpos;
            $length = $endpos - $startpos + 1;
            if ($length             {
                return false;
            }
            $contentlength = $length;
            $ispartial = true;
        }
    }
// send headers
    if ($ispartial)
    {
        header('http/1.1 206 partial content');
        header(content-range: bytes $startpos-$endpos/$filesize);
}
    else
    {
        header(http/1.1 200 ok);
        $startpos = 0;
        $endpos = $contentlength - 1;
    }
    header('pragma: cache');
    header('cache-control: public, must-revalidate, max-age=0');
    header('accept-ranges: bytes');
    header('content-type: ' . $contenttype);
    header('content-length: ' . $contentlength);
if ($forcedownload)
    {
        header('content-disposition: attachment; filename=' . rawurlencode($fancyname). '');//汉字自动转为url编码
  header('content-disposition: attachment; filename=' . $fancyname. '');
    }
    header(content-transfer-encoding: binary);
$buffersize = 2048;
    if ($speedlimit != 0)
    {
        $packettime = floor($buffersize * 1000000 / $speedlimit);
    }
    $bytessent = 0;
    $fp = fopen($filename, rb);
    fseek($fp, $startpos);
    //fpassthru($fp);
while ($bytessent     {
        if ($speedlimit != 0)
        {
            list($usec, $sec) = explode( , microtime());
            $outputtimestart = ((float)$usec + (float)$sec);
        }
        $readbuffersize = $contentlength - $bytessent         $buffer = fread($fp, $readbuffersize);       
        echo $buffer;
        ob_flush();
        flush();
        $bytessent += $readbuffersize;
if ($speedlimit != 0)
        {
            list($usec, $sec) = explode( , microtime());
            $outputtimeend = ((float)$usec + (float)$sec);
$usetime = ((float) $outputtimeend - (float) $outputtimestart) * 1000000;
            $sleeptime = round($packettime - $usetime);
            if ($sleeptime > 0)
            {
                usleep($sleeptime);
            }
        }
    }
return true;
}
 ?>
http://www.bkjia.com/phpjc/728084.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/728084.htmltecharticle复制代码 代码如下: ?php /** * 发送文件 * * @author: legend(legendsky@hotmail.com) * @link: http://www.ugia.cn/?p=109 * @description: send file to client * @version: 1.0...
其它类似信息

推荐信息