Share Coding

Tutorials, Problems, Stuffs …

Serve a file without Direct Link on PHP

Some videos wanted to stream to iPhone, but seems IOS do not support RTMP protocol.
But if those videos exposed on direct link, anyone can download it directly (Insecure).
Thus, the following example is a solution that serve a file without direct link to itself.

 
getFile.php

$file = '/usr/local/videos/video.mp4';
$mime_type = "video/mp4";
if ( is_file($file) ) {
	header("Content-type: $mime_type");
	if( isset($_SERVER['HTTP_RANGE']) ) {
		rangeDownload($file);
	} else {
		header("Content-Length: " . filesize($file));
		readfile($file);
	}
} else {
	// some error...
}

 

function rangeDownload($file) {
	$fp = @fopen($file, 'rb');
	$size   = filesize($file);
	$length = $size;
	$start  = 0;
	$end    = $size - 1;
	header("Accept-Ranges: 0-$length");

	if( isset($_SERVER['HTTP_RANGE']) ) {
		$c_start = $start;
		$c_end   = $end;
		list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);

		if (strpos($range, ',') !== false) {
			header('HTTP/1.1 416 Requested Range Not Satisfiable');
			header("Content-Range: bytes $start-$end/$size");
			exit;
		}
		
		if ($range0 == '-') {
			$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;
		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");
			exit;
		}
		$start  = $c_start;
		$end    = $c_end;
		$length = $end - $start + 1;
		fseek($fp, $start);
		header('HTTP/1.1 206 Partial Content');
	}

	header("Content-Range: bytes $start-$end/$size");
	header("Content-Length: $length");
 
	$buffer = 1024 * 8;
	while( !feof($fp) && ($p = ftell($fp)) <= $end ) {
		if( $p + $buffer > $end ) { $buffer = $end - $p + 1; }
		set_time_limit(0);
		echo fread($fp, $buffer);
		flush();
	}
	fclose($fp);
}
Advertisements

4 responses to “Serve a file without Direct Link on PHP

  1. Website 2013/12/31 at 11:37 am

    I will immediately grasp your rss as I can not in finding your e-mail subscription
    link or newsletter service. Do you’ve any?
    Kindly allow me recognize so that I may just subscribe.
    Thanks.

  2. forex us 2015/09/28 at 11:56 am

    Hi,. can enybody please tell me other platforms that blogger, wordpress, posterous, dupral where I can make blog posts by email?. I need a list of free blog services that allow you to make your post by email..

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: