处理文件加载. 文件下载等相关功能<?php <br />
<br>
class http {<br>
<br>
/**<br>
* http下载文件<br>
*<br>
* Reads a file and send a header to force download it.<br>
* @copyright www.doophp.com<br>
* @param string $file_str File name with absolute path to it<br>
* @param bool $isLarge If True, the large file will be read chunk by chunk into the memory.<br>
* @param string $rename Name to replace the file name that would be downloaded<br>
*/<br>
public static function download($file, $isLarge = false, $rename = NULL){<br>
<br>
if(headers_sent())return false;<br>
<br>
if(!$file) {<br>
exit('Error 404:The file not found!');<br>
}<br>
<br>
if($rename==NULL){<br>
if(strpos($file, '/')===false && strpos($file, '\\')===false)<br>
$filename = $file;<br>
else{<br>
$filename = basename($file);<br>
}<br>
}else{<br>
$filename = $rename;<br>
}<br>
<br>
header('Content-Description: File Transfer');<br>
header('Content-Type: application/octet-stream');<br>
header("Content-Disposition: attachment; filename=\"$filename\"");<br>
header('Expires: 0');<br>
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');<br>
header('Pragma: public');<br>
header('Content-Length: ' . filesize($file));<br>
ob_clean();<br>
flush();<br>
<br>
if($isLarge)<br>
self::readfileChunked($file);<br>
else<br>
readfile($file);<br>
}<br>
<br>
/**<br>
* Read a file and display its content chunk by chunk<br>
*<br>
* @param string $filename<br>
* @param bool $retbytes<br>
* @return mixed<br>
*/<br>
private function readfileChunked($filename, $retbytes = true, $chunkSize = 1024) {<br>
<br>
$buffer = '';<br>
$cnt =0;<br>
<br>
$handle = fopen($filename, 'rb');<br>
if ($handle === false) {<br>
return false;<br>
}<br>
while (!feof($handle)) {<br>
$buffer = fread($handle, $chunkSize);<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>
<br>
return $status;<br>
}<br>
<br>
/**<br>
* HTTP状态信息数组<br>
*<br>
* @var array<br>
*/<br>
protected static $_httpStatus = array(<br>
<br>
'100' => 'Continue',<br>
'101' => 'Switching Protocols',<br>
<br>
'200' => 'OK',<br>
'201' => 'Created',<br>
'202' => 'Accepted',<br>
'203' => 'Non-Authoritative Information',<br>
'204' => 'No Content',<br>
'205' => 'Reset Content',<br>
'206' => 'Partial Content',<br>
<br>
'300' => 'Multiple Choices',<br>
'301' => 'Moved Permanently',<br>
'302' => 'Found',<br>
'303' => 'See Other',<br>
'304' => 'Not Modified',<br>
'305' => 'Use Proxy',<br>
'306' => '(Unused)',<br>
'307' => 'Temporary Redirect',<br>
<br>
'400' => 'Bad Request',<br>
'401' => 'Unauthorized',<br>
'402' => 'Payment Required',<br>
'403' => 'Forbidden',<br>
'404' => 'Not Found',<br>
'405' => 'Method Not Allowed',<br>
'406' => 'Not Acceptable',<br>
'407' => 'Proxy Authentication Required',<br>
'408' => 'Request Timeout',<br>
'409' => 'Conflict',<br>
'410' => 'Gone',<br>
'411' => 'Length Required',<br>
'412' => 'Precondition Failed',<br>
'413' => 'Request Entity Too Large',<br>
'414' => 'Request-URI Too Long',<br>
'415' => 'Unsupported Media Type',<br>
'416' => 'Requested Range Not Satisfiable',<br>
'417' => 'Expectation Failed',<br>
<br>
'500' => 'Internal Server Error',<br>
'501' => 'Not Implemented',<br>
'502' => 'Bad Gateway',<br>
'503' => 'Service Unavailable',<br>
'504' => 'Gateway Timeout',<br>
'505' => 'HTTP Version Not Supported',<br>
);<br>
<br>
/**<br>
* 设置HTTP状态信息<br>
*<br>
* @access public<br>
* @param string $code HTTP 状态编码<br>
* @param string $name HTTP 状态信息<br>
* @return string<br>
*/<br>
public static function httpStatus($code, $text = null) {<br>
//获取http协议<br>
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';<br>
$text = is_null($text) ? self::$_httpStatus[$code] : $text;<br>
$status = "$protocol $code $text";<br>
<br>
header($status);<br>
}<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元