Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

Forums

/

Streming through filesystem

1 reply [Last post]

Hello,

is there a way to stream directly from a file system? for example file="C:\...\test.mp3"

I 'm developing a site where registered users can listen to mp3s. I don't want to give away files from the url (i.e. file="http://www.mysite.com/.../test.mp3") because that way everybody could play this file and not only registered users.

How to you overcome such a problem?

 
The player should request the file from an authentication script.

If the user authenticates, use the X-Sendfile headers to handoff the file serving. The X-Sendfile headers can include a path on the server that the user can never see.

For nginx, but similar code works on Apache2 and LightTPD:

<?php

// nginx-0.8.24 8074 JWMP v4.7.581

// for testing call: http://www.domain.com/path/down.php?file=VideoFileName&token=ABC123&crap=flv&start=4893107

// for the JWMP use: 'file':  encodeURIComponent('down.php?file=VideoFileName&token=ABC123&crap=flv'),
//                   'type': 'http',

$file     = isset($_GET['file'])  ? strval($_GET['file'])  : 'video1';
$start    = isset($_GET['start']) ? intval($_GET['start']) :        0;
$token    = isset($_GET['token']) ? strval($_GET['token']) :       '';
$crap     = isset($_GET['crap'])  ? strval($_GET['crap'])  :    'mp4';
$file     = $file . '.' . $crap;
$initpath = '/path/path';
$path     = '/path/';
$filesize = filesize($initpath . $path . $file);
$size     = $filesize - $start;

//...debugging only
//print "<pre>     File: " . $file . "<br />    Start: " . $start . "<br />    Token: " . $token . "<br />     Path: " . $path . "<br />File Size: " . $filesize . "<br />     Size: " . $size; exit;

//...perform any required security checks, validation and/or stats accounting

//...logging
$filename = 'down.log';

//...check to see if $filename exists, if not, create it.
touch($filename) or die("Unable to create: " . $filename);

//...log file format
$datetime    = "[" . date('d/M/Y:h:i:s O') . "]";
$somecontent = $_SERVER['REMOTE_ADDR'] . "  " . $datetime . "  " . $file . "  " . $start . "  " . $token . "  " . $crap . "  " . $path . "  " . $filesize . "  " . $size . "\r\n";

//...open $filename for append.
$handle = fopen($filename, 'a') or die("Could not open file: " . $filename . "\n");

//...write $somecontent to the open file.
fwrite($handle, $somecontent) or die("Could not write to file: " . $filename . "\n");
$somecontent = "\$_GET:";
foreach(
$_GET as $key => $value)
{
  if($key != 'C')  // ignore this particular $_GET value
  {
    $somecontent .= '  ' . $key . ': ' . $value;
  }
}
$somecontent .= "\r\n";

//...write $somecontent to the open file.
fwrite($handle, $somecontent);

fclose($handle);

//...debugging only
//print "<pre>Range: bytes=" . $start . "-";
//$gets = var_export($_GET, true);
//print "\n\$_GET: " . $gets;
//exit;

//...redirect user to internal location - nginx
header("X-Accel-Redirect: " . $path . $file . "?start=" . $start);

?>