Reading .flv files above 75 MB in JW player by PHP script

software development

#1

Hello,

I made a script that makes my .flv files secure.

I created something like that in .htaccess:

[code]AddType video/x-flv .flv
Options -Indexes
RewriteEngine On

RewriteRule .(flv|zip)$ controller.php [NC,L][/code]

and in controller.php I’m taking the URL checking if it has the hash in the URL which is in database and then I play the video by using
readfile(); function.

readfile function worked well on files to like 30-40MB, but above that .flv files didn’t even play in JW player so I decided to make this script:

[code] function smartReadFile($location, $filename, $mimeType=‘video/x-flv’)
{

	   if(!file_exists($location))
	  { header ("HTTP/1.0 404 Not Found");
	    return;
	  }
	  
	  $size=filesize($location);
	  $time=date('r',filemtime($location));
	  
	  $fm=@fopen($location,'rb');
	  if(!$fm)
	  { header ("HTTP/1.0 505 Internal server error");
	    return;
	  }
	  
	  $begin=0;
	  $end=$size;
	  
	  if(isset($_SERVER['HTTP_RANGE']))
	  { if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches))
	    { $begin=intval($matches[0]);
	      if(!empty($matches[1]))
	        $end=intval($matches[1]);
	    }
	  }
	  
	  if($begin>0||$end<$size)
	    header('HTTP/1.0 206 Partial Content');
	  else
	    header('HTTP/1.0 200 OK');  
	  
	  header("Content-Type: $mimeType"); 
      header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
	  header('Pragma: no-cache');  
	  header('Accept-Ranges: bytes');
	  header('Content-Length:'.($end-$begin));
	  header("Content-Range: bytes $begin-$end/$size");
	  header("Content-Disposition: inline; filename=$filename");
	  header("Content-Transfer-Encoding: binary\n");
	  header("Last-Modified: $time");
	  header('Connection: close');  
	  
	  $cur=$begin;
	  fseek($fm,$begin,0);

	  while(!feof($fm)&&$cur<$end&&(connection_status()==0))
	  { 
	  	echo fread($fm,min(1024*2,$end-$cur));
	    $cur+=1024*2;
	    set_time_limit(30);
	     
	  }
	}[/code]

And now I can play videos above even 70MB, but at about 5-7 minutes download process is being stopped.

Anyone know why could it happen? I read that fread function was taking lot’s of RAM, so I chunked as you can see file to only 2 MB, and there are still problems even tho it should take ONLY 2 MB of RAM. So what’s the problem?

Any suggestions please.


#2

"well look’s like this may be the problem:
“In Apache, the timeout parameter of 300 seconds means that if there is “no action” (or no next action) for 300 seconds, the session is timed out.”

is that possible to change it somehow in DreamHost shared hosting to 10 minutes?
or
is that possible to send from JW player at least once every 1 minute something to the .flv file that could keep connection?

best regards!


#3

You said that you have a hash in the database? can you provide a zip file? I dont understand… On my account I dont seem to have a limit.


#4

everyone have limit on php script execution of 5 minute in httpd.conf, i changed value there and now it works fine. httpd.conf is over php.ini and any other functions like set_time_limit.