function readVideo($file) { header("Content-type: video/mp4"); $fp = @fopen($file, 'rb'); $size = filesize($file); // File size $length = $size; // Content length $start = 0; // Start byte $end = $size - 1; // End byte // header("Accept-Ranges: 0-$length"); header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); if (isset($_SERVER['HTTP_RANGE'])) { $c_end = $end; // Extract the range string list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); // Make sure the client hasn't sent us a multibyte range if (strpos($range, ',') !== false) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); exit; } if ($range[0] == '-') { $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; // Validate the requested range and return // an error if it's not correct. 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"); // (?) Echo some info to the client? exit; } $start = $c_start; $end = $c_end; $length = $end - $start + 1; // Calculate new content length fseek($fp, $start); header('HTTP/1.1 206 Partial Content'); header("Content-Range: bytes $start-$end/$size"); header("Content-Length: $length"); // Start buffered download $buffer = 1024 * 8; while(!feof($fp) && ($p = ftell($fp)) $end) { $buffer = $end - $p + 1; } set_time_limit(0); // Reset time limit for big files echo fread($fp, $buffer); // Free up memory. //Otherwise large files will trigger PHP's memory limit. flush(); } fclose($fp); } else { header("Content-Range: bytes $start-$end/$size"); header("Content-Length: $length"); readfile($file); } }
参与评论
手机查看
返回顶部