处理文件加载. 文件下载等相关功能

jerry PHP 2015年11月18日 收藏
处理文件加载. 文件下载等相关功能
  1. <?php

  2. class http {

  3.     /**
  4.      * http下载文件
  5.      *
  6.      * Reads a file and send a header to force download it.
  7.      * @copyright www.doophp.com
  8.      * @param string $file_str File name with absolute path to it
  9.      * @param bool $isLarge If True, the large file will be read chunk by chunk into the memory.
  10.      * @param string $rename Name to replace the file name that would be downloaded
  11.      */
  12.     public static function download($file, $isLarge = false, $rename = NULL){

  13.         if(headers_sent())return false;

  14.         if(!$file) {
  15.             exit('Error 404:The file not found!');
  16.         }

  17.         if($rename==NULL){
  18.             if(strpos($file, '/')===false && strpos($file, '\\')===false)
  19.                 $filename = $file;
  20.             else{
  21.                 $filename = basename($file);
  22.             }
  23.         }else{
  24.             $filename = $rename;
  25.         }

  26.         header('Content-Description: File Transfer');
  27.         header('Content-Type: application/octet-stream');
  28.         header("Content-Disposition: attachment; filename=\"$filename\"");
  29.         header('Expires: 0');
  30.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  31.         header('Pragma: public');
  32.         header('Content-Length: ' . filesize($file));
  33.         ob_clean();
  34.         flush();

  35.         if($isLarge)
  36.             self::readfileChunked($file);
  37.         else
  38.             readfile($file);
  39.     }

  40.     /**
  41.      * Read a file and display its content chunk by chunk
  42.      *
  43.      * @param string $filename
  44.      * @param bool $retbytes
  45.      * @return mixed
  46.      */
  47.     private function readfileChunked($filename, $retbytes = true, $chunkSize = 1024) {

  48.         $buffer = '';
  49.         $cnt =0;

  50.         $handle = fopen($filename, 'rb');
  51.         if ($handle === false) {
  52.             return false;
  53.         }
  54.         while (!feof($handle)) {
  55.             $buffer = fread($handle, $chunkSize);
  56.             echo $buffer;
  57.             ob_flush();
  58.             flush();
  59.             if ($retbytes) {
  60.                 $cnt += strlen($buffer);
  61.             }
  62.         }
  63.         $status = fclose($handle);
  64.         if ($retbytes && $status) {
  65.             return $cnt; // return num. bytes delivered like readfile() does.
  66.         }

  67.         return $status;
  68.     }

  69.     /**
  70.      * HTTP状态信息数组
  71.      *
  72.      * @var array
  73.      */
  74.     protected static $_httpStatus = array(

  75.         '100' => 'Continue',
  76.         '101' => 'Switching Protocols',

  77.         '200' => 'OK',
  78.         '201' => 'Created',
  79.         '202' => 'Accepted',
  80.         '203' => 'Non-Authoritative Information',
  81.         '204' => 'No Content',
  82.         '205' => 'Reset Content',
  83.         '206' => 'Partial Content',

  84.         '300' => 'Multiple Choices',
  85.         '301' => 'Moved Permanently',
  86.         '302' => 'Found',
  87.         '303' => 'See Other',
  88.         '304' => 'Not Modified',
  89.         '305' => 'Use Proxy',
  90.         '306' => '(Unused)',
  91.         '307' => 'Temporary Redirect',

  92.         '400' => 'Bad Request',
  93.         '401' => 'Unauthorized',
  94.         '402' => 'Payment Required',
  95.         '403' => 'Forbidden',
  96.         '404' => 'Not Found',
  97.         '405' => 'Method Not Allowed',
  98.         '406' => 'Not Acceptable',
  99.         '407' => 'Proxy Authentication Required',
  100.         '408' => 'Request Timeout',
  101.         '409' => 'Conflict',
  102.         '410' => 'Gone',
  103.         '411' => 'Length Required',
  104.         '412' => 'Precondition Failed',
  105.         '413' => 'Request Entity Too Large',
  106.         '414' => 'Request-URI Too Long',
  107.         '415' => 'Unsupported Media Type',
  108.         '416' => 'Requested Range Not Satisfiable',
  109.         '417' => 'Expectation Failed',

  110.         '500' => 'Internal Server Error',
  111.         '501' => 'Not Implemented',
  112.         '502' => 'Bad Gateway',
  113.         '503' => 'Service Unavailable',
  114.         '504' => 'Gateway Timeout',
  115.         '505' => 'HTTP Version Not Supported',
  116.     );

  117.     /**
  118.      * 设置HTTP状态信息
  119.      *
  120.      * @access public
  121.      * @param string $code  HTTP 状态编码
  122.      * @param string $name  HTTP 状态信息
  123.      * @return string
  124.      */
  125.     public static function httpStatus($code, $text = null) {
  126.         //获取http协议
  127.         $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
  128.         $text = is_null($text) ? self::$_httpStatus[$code] : $text;
  129.         $status = "$protocol $code $text";

  130.         header($status);
  131.     }
  132. }