来自 澳门金莎娱乐手机版 2019-09-27 01:08 的文章
当前位置: 金沙澳门官网网址 > 澳门金莎娱乐手机版 > 正文

澳门金莎娱乐手机版缓存协商,分享多少个超好

<?php教程 

复制代码 代码如下:

002 $file_path = './download/download_cn.rar'; 

<?php
/**
 * 发送文件
 *
 * @author: legend(legendsky@hotmail.com)
 * @link:
 * @description: send file to client
 * @version: 1.0
 *
 * @param string   $fileName      文件名称或路径
 * @param string   $fancyName     自定义的文件名,为空则使用filename
 * @param boolean  $forceDownload 是否强制下载
 * @param integer  $speedLimit    速度限制,单位为字节,0为不限制,不支持windows服务器
 * @param string   $$contentType  文件类型,默认为application/octet-stream
 *
 * @return boolean
 */
function sendFile($fileName, $fancyName = '', $forceDownload = true, $speedLimit = 0, $contentType = '')
{
    if (!is_readable($fileName))
    {
        header("HTTP/1.1 404 Not Found");
        return false;
    }

003   

    $fileStat = stat($fileName);
    $lastModified = $fileStat['mtime'];

004 //使用方法 

    $md5 = md5($fileStat['mtime'] .'='. $fileStat['ino'] .'='. $fileStat['size']);
    $etag = '"' . $md5 . '-' . crc32($md5) . '"';

005 downFile($file_path); 

    header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT');
    header("ETag: $etag");

006   

    if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified)
    {
        header("HTTP/1.1 304 Not Modified");
        return true;
    }

007 // 服务器文件路径,下载文件名字(默认为服务器文件名),是否许可用户下载方式(默认可选),速度限制(默认自动),文件类型(默认所有) 

    if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $lastModified)
    {
        header("HTTP/1.1 304 Not Modified");
        return true;
    }

008 function downFile($fileName, $fancyName = '', $forceDownload = true, $speedLimit = 0, $contentType = '') {  

    if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&  $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
    {
        header("HTTP/1.1 304 Not Modified");
        return true;
    }

009     if (!is_readable($fileName))  

    if ($fancyName == '')
    {
        $fancyName = basename($fileName);
    }

010     {  

    if ($contentType == '')
    {
        $contentType = 'application/octet-stream';
    }

011         header("HTTP/1.1 404 Not Found");  

    $fileSize = $fileStat['size'];  

012         return false;  

    $contentLength = $fileSize;
    $isPartial = false;

013     }  

    if (isset($_SERVER['HTTP_RANGE']))
    {
        if (preg_match('/^bytes=(d*)-(d*)$/', $_SERVER['HTTP_RANGE'], $matches))
        {   
            $startPos = $matches[1];
            $endPos = $matches[2];

014     

            if ($startPos == '' && $endPos == '')
            {
                return false;
            }

015     $fileStat = stat($fileName);  

            if ($startPos == '')
            {
                $startPos = $fileSize - $endPos;
                $endPos = $fileSize - 1;
            }
            else if ($endPos == '')
            {
                $endPos = $fileSize - 1;
            }

016     $lastModified = $fileStat['mtime'];  

            $startPos = $startPos < 0 ? 0 : $startPos;
            $endPos = $endPos > $fileSize - 1 ? $fileSize - 1 : $endPos;

017     

            $length = $endPos - $startPos + 1;

018     $md5 = md5($fileStat['mtime'] .'='. $fileStat['ino'] .'='. $fileStat['size']);  

            if ($length < 0)
            {
                return false;
            }

019     $etag = '"' . $md5 . '-' . crc32($md5) . '"';  

            $contentLength = $length;
            $isPartial = true;
        }
    }

020     

    // send headers
    if ($isPartial)
    {
        header('HTTP/1.1 206 Partial Content');
        header("Content-Range: bytes $startPos-$endPos/$fileSize");

021     header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT');  

    }
    else
    {
        header("HTTP/1.1 200 OK");
        $startPos = 0;
        $endPos = $contentLength - 1;
    }

022     header("ETag: $etag");  

    header('Pragma: cache');
    header('Cache-Control: public, must-revalidate, max-age=0');
    header('Accept-Ranges: bytes');
    header('Content-type: ' . $contentType);
    header('Content-Length: ' . $contentLength);

023     

    if ($forceDownload)
    {
        header('Content-Disposition: attachment; filename="' . rawurlencode($fancyName). '"');//汉字自动转为URL编码
  header('Content-Disposition: attachment; filename="' . $fancyName. '"');
    }

024     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified)  

    header("Content-Transfer-Encoding: binary");

025     {  

    $bufferSize = 2048;

026         header("HTTP/1.1 304 Not Modified");  

    if ($speedLimit != 0)
    {
        $packetTime = floor($bufferSize * 1000000 / $speedLimit);
    }

027         return true;  

    $bytesSent = 0;
    $fp = fopen($fileName, "rb");
    fseek($fp, $startPos);

028     }  

    //fpassthru($fp);

029     

    while ($bytesSent < $contentLength && !feof($fp) && connection_status() == 0 )
    {
        if ($speedLimit != 0)
        {
            list($usec, $sec) = explode(" ", microtime());
            $outputTimeStart = ((float)$usec + (float)$sec);
        }

030     if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $lastModified)  

        $readBufferSize = $contentLength - $bytesSent < $bufferSize ? $contentLength - $bytesSent : $bufferSize;
        $buffer = fread($fp, $readBufferSize);       

031     {  

        echo $buffer;

032         header("HTTP/1.1 304 Not Modified");  

        ob_flush();
        flush();

033         return true;  

        $bytesSent += $readBufferSize;

034     }  

        if ($speedLimit != 0)
        {
            list($usec, $sec) = explode(" ", microtime());
            $outputTimeEnd = ((float)$usec + (float)$sec);

035     

            $useTime = ((float) $outputTimeEnd - (float) $outputTimeStart) * 1000000;
            $sleepTime = round($packetTime - $useTime);
            if ($sleepTime > 0)
            {
                usleep($sleepTime);
            }
        }
    }    

036     if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&  $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)  

    return true;
}
 ?>

037     {  

您可能感兴趣的文章:

  • php输出xml必须header的解决方法
  • PHP错误Warning: Cannot modify header information - headers already sent by解决方法
  • PHP header()函数常用方法总结
  • php用header函数实现301跳转代码实例
  • Php header()函数语法及使用代码
  • php header功能的使用
  • 分享PHP header函数使用教程
  • 浅析php header 跳转
  • PHP 使用header函数设置HTTP头的示例解析 表头
  • 探讨php中header的用法详解
  • php 模拟get_headers函数的代码示例
  • PHP header()函数使用详细(301、404等错误设置)
  • PHP利用header跳转失效的解决方法

038         header("HTTP/1.1 304 Not Modified");  

039         return true;  

040     }  

041     

042     if ($fancyName == '')  

043     {  

044         $fancyName = basename($fileName);  

045     }  

046     

047     if ($contentType == '')  

048     {  

049         $contentType = 'application/octet-stream';  

050     }  

051     

052     $fileSize = $fileStat['size'];  

053     

054     $contentLength = $fileSize;  

055     $isPartial = false;  

056     

057     if (isset($_SERVER['HTTP_RANGE']))  

058     {  

059         if (preg_match('/^bytes=(d*)-(d*)$/', $_SERVER['HTTP_RANGE'], $matches))  

060         {  

061             $startPos = $matches[1];  

062             $endPos = $matches[2];  

063     

064             if ($startPos == '' && $endPos == '')  

065             {  

066                 return false;  

067             }  

068     

069             if ($startPos == '')  

070             {  

071                 $startPos = $fileSize - $endPos;  

072                 $endPos = $fileSize - 1;  

073             }  

074             else if ($endPos == '')  

075             {  

076                 $endPos = $fileSize - 1;  

077             }  

078     

079             $startPos = $startPos < 0 ? 0 : $startPos;  

080             $endPos = $endPos > $fileSize - 1 ? $fileSize - 1 : $endPos;  

081     

082             $length = $endPos - $startPos + 1;  

本文由金沙澳门官网网址发布于澳门金莎娱乐手机版,转载请注明出处:澳门金莎娱乐手机版缓存协商,分享多少个超好

关键词: