/** * 输出、文件下载 * 默认以附件方式下载;$download为false时则为输出文件 */ function file_put_out($file, $download = false) { if (!is_file($file)) { show_json('not a file!'); } if (!file_exists($file)) { show_json('file not exists', false); } if (!is_readable($file)) { show_json('file not readable', false); } set_time_limit(0); ob_clean(); //清除之前所有输出缓冲 TODO $mime = get_file_mime(get_path_ext($file)); if ($download || strstr($mime, 'application/') && $mime != 'application/x-shockwave-flash') { //下载或者application则设置下载头 $filename = get_path_this($file); //解决在IE中下载时中文乱码问题 if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT']) || preg_match('/Trident/', $_SERVER['HTTP_USER_AGENT'])) { if ($GLOBALS['config']['system_os'] != 'windows') { //win主机 ie浏览器;中文文件下载urlencode问题 $filename = str_replace('+', '%20', urlencode($filename)); } } header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment;filename=" . $filename); } else { //缓存文件 header('Expires: ' . date('D, d M Y H:i:s', time() + 3600 * 24 * 20) . ' GMT'); header("Cache-Pragma: public"); header("Cache-Control: cache, must-revalidate"); if (isset($_SERVER['If-Modified-Since']) && strtotime($_SERVER['If-Modified-Since']) == filemtime($file)) { header('HTTP/1.1 304 Not Modified'); header('Last-Modified: ' . date('D, d M Y H:i:s', filemtime($file)) . ' GMT'); //304 exit; } else { header('Last-Modified: ' . date('D, d M Y H:i:s', filemtime($file)) . ' GMT', true, 200); } $etag = '"' . md5(date('D, d M Y H:i:s', filemtime($file))) . '"'; if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { header('HTTP/1.1 304 Not Modified'); header('Etag: ' . $etag); exit; } else { header('Etag: ' . $etag); } } header("X-Powered-By: kodExplorer."); header("Content-Type: " . $mime); header("Accept-Ranges: bytes"); $size = get_filesize($file); $length = $size; // Content length $start = 0; // Start byte $end = $size - 1; // End byte if (isset($_SERVER['HTTP_RANGE']) || isset($_GET['start'])) { //分段请求;视频拖拽 $c_start = $start; $c_end = $end; if (isset($_GET['start'])) { $c_start = intval($_GET['start']); } else { //range list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); if (strpos($range, ',') !== false) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes {$start}-{$end}/{$size}"); exit; } if ($range == '-') { $c_start = $size - substr($range, 1); } else { $range = explode('-', $range); $c_start = $range[0]; $c_end = isset($range[1]) && is_numeric($range[1]) ? $range[1] : $size; } $c_end = $c_end > $end ? $end : $c_end; if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes {$start}-{$end}/{$size}"); exit; } } $start = $c_start; $end = $c_end; $length = $end - $start + 1; header('HTTP/1.1 206 Partial Content'); } header("Content-Range: bytes {$start}-{$end}/{$size}"); header("Content-Length: " . $length); //header("X-Sendfile: $file");exit; if (!($fp = @fopen($file, "rb"))) { exit; } fseek($fp, $start); while (!feof($fp)) { set_time_limit(0); print fread($fp, 1024 * 4); //输出文件 flush(); ob_flush(); } fclose($fp); }
/** * 文件代理输出 */ function file_proxy_out($file) { if (!file_exists($file)) { show_json('file not exists', false); } $mime = get_file_mime(get_path_ext($file)); header("Cache-Control:public"); header("Content-Type:" . $mime); header("Content-Length: " . filesize($file)); //输出总长 $fp = fopen($file, "rb"); set_time_limit(0); while (!feof($fp)) { print fread($fp, 1024 * 8); //输出文件 flush(); ob_flush(); } fclose($fp); exit; }
/** * Output, file download * Default attachment download; $ download is false when compared to the output file */ function file_put_out($file, $download = false) { if (!is_file($file)) { show_json('not a file!'); } set_time_limit(0); //ob_clean();//Clear until all output buffers if (!file_exists($file)) { show_json('file not exists', false); } if (isset($_SERVER['HTTP_RANGE']) && $_SERVER['HTTP_RANGE'] != "" && preg_match("/^bytes=([0-9]+)-\$/i", $_SERVER['HTTP_RANGE'], $match) && $match[1] < $fsize) { $start = $match[1]; } else { $start = 0; } $size = get_filesize($file); $mime = get_file_mime(get_path_ext($file)); if ($download || strstr($mime, 'application/')) { //Download or download application is set head $filename = get_path_this($file); //Download IE resolve when Chinese garbage problem if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT']) || preg_match('/Trident/', $_SERVER['HTTP_USER_AGENT'])) { if ($GLOBALS['config']['system_os'] != 'windows') { //win the host ie browser; Chinese file download urlencode problem $filename = str_replace('+', '%20', urlencode($filename)); } } header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment;filename=" . $filename); } header("Cache-Control: public"); header("X-Powered-By: kodExplorer."); header("Content-Type: " . $mime); if ($start > 0) { header("HTTP/1.1 206 Partial Content"); header("Content-Ranges: bytes" . $start . "-" . ($size - 1) . "/" . $size); header("Content-Length: " . ($size - $start)); } else { header("Accept-Ranges: bytes"); header("Content-Length: {$size}"); } $fp = fopen($file, "rb"); fseek($fp, $start); while (!feof($fp)) { print fread($fp, 1024 * 8); //Output File flush(); ob_flush(); } fclose($fp); }
/** * 输出、文件下载 * 默认以附件方式下载;$download为false时则为输出文件 */ function file_put_out($file, $download = false) { if (!is_file($file)) { print 'not a file!'; } set_time_limit(0); //ob_clean();//清除之前所有输出缓冲 if (!file_exists($file)) { print 'file not exists'; } if (isset($_SERVER['HTTP_RANGE']) && $_SERVER['HTTP_RANGE'] != "" && preg_match("/^bytes=([0-9]+)-\$/i", $_SERVER['HTTP_RANGE'], $match) && $match[1] < $fsize) { $start = $match[1]; } else { $start = 0; } $size = get_filesize($file); header("Cache-Control: public"); header("X-Powered-By: HackExplorer."); if ($download) { header("Content-Type: application/octet-stream"); $filename = $file; //解决在IE中下载时中文乱码问题 if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { $filename = str_replace('+', '%20', urlencode($filename)); } header("Content-Disposition: attachment;filename=" . $filename); } else { $mime = get_file_mime(get_path_ext($file)); header("Content-Type: " . $mime); } if ($start > 0) { header("HTTP/1.1 206 Partial Content"); header("Content-Ranges: bytes" . $start . "-" . ($size - 1) . "/" . $size); header("Content-Length: " . ($size - $start)); } else { header("Accept-Ranges: bytes"); header("Content-Length: {$size}"); } $fp = fopen($file, "rb"); fseek($fp, $start); while (!feof($fp)) { print fread($fp, 1024 * 8); //输出文件 flush(); ob_flush(); } fclose($fp); }
public function fileProxy() { $mime = get_file_mime(get_path_ext($this->path)); if ($mime == 'text/plain') { //Then turn text encoding $filecontents = file_get_contents($this->path); $charset = get_charset($filecontents); if ($charset != '' || $charset != 'utf-8') { $filecontents = mb_convert_encoding($filecontents, 'utf-8', $charset); } echo $filecontents; return; } file_put_out($this->path); }