您的位置 首页 php

php大文件下载处理

 	set_time_limit(0);
	ini_set('max_execution_time',0);

//get the http get values and set them as php variables
	

//download the recordings
	if ($_GET['a'] == "download") {
		session_cache_limiter('public');
		if ($_GET['type'] = "rec") {
			$ File _path = $_SESSION['switch']['recordings']['dir'].'/'.base64_decode($_GET['filename']);
			$filesize = filesize($file_path);
			if (file_exists($file_path)) {
				// $fd = fopen($file_path, "rb");
				if ($_GET['t'] == "bin") {
					// header("Content-Type: application/force-download");
					header("Content-Type: application/octet-stream");
					// header("Content-Type: application/download");
					// header("Content-Description: File Transfer");
					header('Content-Disposition: attachment; filename="'.basename($file_path).'"');
				}
				else {
					$file_ext =  substr (base64_decode($_GET['filename']), -3);
					if ($file_ext == "wav") {
						header("Content-Type: audio/x-wav");
					}
					if ($file_ext == "mp3") {
						header("Content-Type: audio/mp3");
					}
					if ($file_ext == "avi") {
						header("Content-Type: video/avi");
					}
					if ($file_ext == " mp4 ") {
						header("Content-Type: video/mp4");
					}
					
				}
				// header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
				// header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
				header("Content-Length: " . $filesize);
				ob_clean();
				// fpassthru($fd);
				//针对大文件,规定每次读取文件的字节数为4096字节,直接输出数据
			    $read_buffer = 4096;
			    $handle = fopen($file_path, 'rb');
			    //总的缓冲的字节数
			    $sum_buffer = 0;
			    //只要没到文件尾,就一直读取
			    while(!feof($handle) && $sum_buffer<$filesize) {
			        echo fread($handle,$read_buffer);
			        $sum_buffer += $read_buffer;
			    }

			    //关闭句柄
			     fclose ($handle);
			}
		}
		exit;
	}  

文章来源:智云一二三科技

文章标题:php大文件下载处理

文章地址:https://www.zhihuclub.com/77589.shtml

关于作者: 智云科技

热门文章

网站地图