Skip to content

Instantly share code, notes, and snippets.

@watagashi
Created June 4, 2013 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watagashi/5709995 to your computer and use it in GitHub Desktop.
Save watagashi/5709995 to your computer and use it in GitHub Desktop.
<?php
// put your code here
/*
header("HTTP/1.0 200");
header("Content-Type: video/quicktime;");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
*/
//ob_clean();
$file = $_GET['file'];
$file_size = filesize( $file ) ;
header( "Accept-Ranges: bytes" ) ;
header("Content-Type: video/mp4");
//header("Content-Type: video/quicktime");
//header("Content-Type: video/quicktime");
if( isset( $_SERVER['HTTP_RANGE'] ) ) {
// 二回目以降のリクエスト
// RangeRequest な処理
// Content-Length, Content-Range, 206 Partial Content, Last-Modified
// $_SERVER['HTTP_RANGE'] の値は bytes=0-999999 のような形式なので、開始・終了値を取り出して、Content-Range を生成する
list( $range_offset, $range_limit ) = sscanf( $_SERVER['HTTP_RANGE'], "bytes=%d-%d" ) ;
$content_range = sprintf( "bytes %d-%d/%d", $range_offset, $range_limit, $file_size ) ;
// オフセット位置 0 なので、長さとしては + 1
$content_length = $range_limit - $range_offset + 1 ;
// コンテンツの更新日時
// プログラムファイルの更新日時が返るので、コンテンツの更新日時を返したい場合は別途処理が必要(だと思う)
// 常にダウンロードさせるのであれば、GMT で現在日時を返せば良い
$last_modified = $_SERVER['HTTP_IF_UNMODIFIED_SINCE'] ;
// 第4、5引数でファイル切り出し範囲を指定、変数に全部入れる
// メモリ的余裕がなければ、fseek() でポインタを移動して fread() などする
// fseek( $fp, $range_offset, SEEK_SET ) ;
$content = file_get_contents( $file, FILE_BINARY, null, $range_offset, $range_limit ) ;
header( "HTTP/1.1 206 Partial Content" ) ;
header( "Content-Lenth: " . $content_length ) ;
header( "Content-Range: " . $content_range ) ;
header( "Last-Modified: " . $last_modified ) ;
} else {
// 一回目のリクエスト
// Content-Length のヘッダと、ファイル全体をレスポンス
$content_length = $file_size ;
// 処理能力やメモリ的に余裕があるなら、変数に全部入れるのが楽
// 不安なら fread() とか使う
$content = file_get_contents( $file ) ;
header( "Content-Length: " . $content_length ) ;
}
echo $content ;
//echo fread( $fp, $content_length ) ;
/*
http_send_content_type("video/mp4");
http_throttle(0.1, 2048);
//http_send_file($_GET['file']);
http_send_file("20130602.mp4");
*/
//flush();
//readfile($_GET['file']);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment