- /**
- * 文件下载
- * @param $filepath 文件路径
- * @param $filename 文件名称
- *@B5教程网 www.bcty365.com
- */
- private function downLocalFile($filepath, $filename = '')
- {
- if (!$filename) {
- $filename = basename($filepath);
- }
- $doc_path = str_ireplace(str_replace("\\", "/", $_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_FILENAME']);
- $realpath = $doc_path . $filepath;
- if (!file_exists($realpath)) {
- header('HTTP/1.1 404 Not Found');
- echo "Error: 404 Not Found.(server file path error)<!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding -->";
- exit;
- }
- $filetype = strtolower(substr(strrchr($filename, "."), 1)); //获取文件扩展名
- $filesize = sprintf("%u", filesize($realpath));
- if (ob_get_length() !== false) {
- @ob_end_clean();
- }
- header('Pragma: public');
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
- header('Cache-Control: no-store, no-cache, must-revalidate');
- header('Cache-Control: pre-check=0, post-check=0, max-age=0');
- header('Content-Transfer-Encoding: binary');
- header('Content-Encoding: none');
- header('Content-type: ' . $filetype);
- if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) {
- //for IE
- header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
- } else {
- header('Content-Disposition: attachment; filename="' . $filename . '"');
- }
- header('Content-length: ' . $filesize);
- readfile($realpath);
- exit;
- }