Browse Source

update:下载类相关数据

Zhu Jinhui 7 years ago
parent
commit
d51c6c739f
1 changed files with 54 additions and 51 deletions
  1. 54 51
      src/Library/Download.php

+ 54 - 51
src/Library/Download.php

@@ -98,59 +98,62 @@ class Download
 		//设定不限制时间
 		ignore_user_abort(false);
 		set_time_limit(0);
-		if(file_exists($filePath))
+		//转换文件路为GBK,避免无法访问中文文件
+		$filePath = toGBK($filePath);
+		if(!file_exists($filePath))
 		{
-			if($fileName == '')
-			{
-				$fileName = basename($filePath);
-			}
-			$file = fopen($filePath, "r"); // 打开文件
-			// 输入文件标签
-			header('Cache-Control: max-age=2592000');
-			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');
-			if($mime != '' && $mime != 'unlink')
-			{
-				header("Content-type: {$mime}");
-			}
-			else
-			{
-				header("Content-type: application/octet-stream");
-			}
-			header('Content-Encoding: none');
-			header('Content-Transfer-Encoding: binary');
-			header("Accept-Ranges: bytes");
-			header("Accept-Length: ".  filesize($filePath));
-			$fileName = toGBK($fileName);
-			if($view == 'download')
-			{
-				header("Content-Disposition: attachment; filename=". $fileName);
-			}
-			else
+			die('File '. $filePath .' does not exist.');
+		}
+		if($fileName == '')
+		{
+			$fileName = basename($filePath);
+		}
+		$file = fopen($filePath, "r"); // 打开文件
+		// 输入文件标签
+		header('Cache-Control: max-age=2592000');
+		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');
+		if($mime != '' && $mime != 'unlink')
+		{
+			header("Content-type: {$mime}");
+		}
+		else
+		{
+			header("Content-type: application/octet-stream");
+		}
+		header('Content-Encoding: none');
+		header('Content-Transfer-Encoding: binary');
+		header("Accept-Ranges: bytes");
+		header("Accept-Length: ".  filesize($filePath));
+		$fileName = toGBK($fileName);
+		if($view == 'download')
+		{
+			header("Content-Disposition: attachment; filename=". $fileName);
+		}
+		else
+		{
+			header('Content-Disposition: inline;filename="'.$fileName.'"');
+		}
+		//输出固定长度的文件避免文件过大导致无法下载
+		$chunk = 16384;
+		$speed = 1000;
+		//#$speed = 0;
+		$sleep = $speed ? floor(( $chunk / ($speed*1024))*1000000) : 0;
+		do
+		{
+			$buf = fread($file, $chunk);
+			$sent += strlen($buf);
+			echo $buf;
+			ob_flush();
+			flush();
+			usleep($sleep);
+			if(strlen($buf) ==0)
 			{
-				header('Content-Disposition: inline;filename="'.$fileName.'"');
+				break;
 			}
-			//输出固定长度的文件避免文件过大导致无法下载
-			$chunk = 16384;
-			$speed = 1000;
-			//#$speed = 0;
-			$sleep = $speed ? floor(( $chunk / ($speed*1024))*1000000) : 0;
-			do
-			{
-				$buf = fread($file, $chunk);
-				$sent += strlen($buf);
-				echo $buf;
-				ob_flush();
-				flush();
-				usleep($sleep);
-				if(strlen($buf) ==0)
-				{
-					break;
-				}
-			}while(true);
-			fclose($file);
-			exit();
-		}
+		}while(true);
+		fclose($file);
+		exit();
 	}
 }