Justin T. Watts
Justin T. Watts

Reputation: 565

Streaming MP4s with PHP

I'm trying to stream mp4 files through FTP and PHP but only mp3s will stream, the mp4s will download but not stream is there a special format that mp4s MUST be in to stream?

This is the code I'm trying:

$response =  ftp_raw($ftp, 'SIZE '.$_GET['path']); 
$size = floatval(str_replace('213 ', '', $response[0]));
if(substr($_GET['path'], -4) == '.mp3') {
  header('Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3');
}
if(substr($_GET['path'], -4) == ".mp4") {
  header('Content-Type: video/mpeg');
}
header('Content-Encoding: chunked');
header('Connection: keep-alive');
header('Content-Disposition: inline; filename="'. basename($_GET["path"]). '"');
header('Content-Description: File Transfer'); 
header("Content-Length: $size");
header('Content-Transfer-Encoding: binary'); 
header('X-Pad: avoid browser bug');
header('Pragma: public');
header('Expires: -1');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Content-Range: bytes 0-'.$size); 
header('Accept-Ranges: bytes');
$out = fopen('php://output', 'w');
if (!ftp_fget($ftp, $out, $_GET['path'], FTP_BINARY)) die('Unable to get file: ' . $remote_file);
fclose($out);

Upvotes: 2

Views: 1380

Answers (1)

asiby
asiby

Reputation: 3399

You need a stream server of some sort. If you really want to do that with PHP, which I stongly discourage, then you will need to open a socket connection and write chunks of data that you will be sequentially reading from the file ... in a loop until you go through the entire file.

Considering the fact that you should be also considering concurrency and proper memory management, and considering how php is (overhead, not thread friendly ... or so I have been told, ...), just don't do that with PHP.

There are plenty of open source streaming solutions around and their will meet your need.

Yep, PHP is not really meant for that ... though it is possible in theory.

Anyone please correct me if I wrong.

Upvotes: 1

Related Questions