transfer a file bigger than 500mb - php

i created a file with php that can stream a file in a server but the only problem i am having is the resume and i can download only with 1 parallel using IDM i have been searching for a week without finding any thing that helps this is my code
<?php
$file = 'http://www.affymetrix.com/support/downloads/demo_data/Demo_Data_Barley_MAS5.zip';
function get_size($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
return intval($size);
}
$fp = #fopen($file, 'rb');
$size = get_size($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=Demo_Data_Barley_MAS5.zip");
//header('Content-type: video/mp4');
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 ($range == '-') {
$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);
exit();
?>
i am really thank full if some one can help me out here

Use following gist source code for this: https://gist.github.com/kijin/9735300
(im sorry for that linked gist source code annotated by Korean)
Translation of header:
supports:
1. do not break filename when use UTF-8 filename.
2. remove or replace if included unknown character by OS.
3. Add Cache-Control, Expires header when you want to use cache.
4. Fix the download error when use cache and IE <= 8.
5. Support resume download (automatically detect Range header, auto generate Accept-Ranges header)
6. Fix memory leak when download large file.
7. Can limit download speed.
How to use: send_attachment('filename that provide to client', 'file path', [period of the caching], [speed limit]);
this example is download 'foo.jpg' from server to client named 'photo.jpg'
send_attachment('photo.jpg', '/srv/www/files/uploads/foo.jpg')
Return: true when successfully sent else false.
Warning: 1. please execute 'exit' when end the transfer.
2. don't ensure that php version is very low(< 5.1) or not UTF-8 environment.
3. speed limitation is very dangerous when you using FastCGI/FPM. recommend to use web server's speed limitation.
4. some android versions does not support UTF-8 encoding.
Note: I recommend to use X-Accel-Redirect(nginx) or X-Sendfile(apache) header when transfer a big file. If you transfer big file on the php interpreter, the php have many load and it works very inefficiently.
Add: here is the code that you want to do - https://gist.github.com/ssut/a3d97c7a35e5458687ed
(note: I'm very poor in english, some translation is imprecise)
I tested this code on nginx + php 5.5(fpm) environment and this is test code: send_attachment('ubuntu.iso', 'http://ftp.daum.net/ubuntu-releases/trusty/ubuntu-14.04-server-amd64.iso');
IDM screenshot:

Related

How to open video/mp4 with php

I'm trying to read mp4 files with PHP, my initial code was
$file = 'https://s3-sa-east-1.amazonaws.com/onlytestes/video.mp4';
header('Content-type: video/mp4');
readfile($file);
But that way I couldn't use the length bar of the video, skip or even go back, until the video is 100% loaded.
Of course when I read the file directly (video.mp4) everything is fine.
I solved this problem with the following code:
$request = 'video.mp4';
$file = $request;
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
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 ($range == '-') {
$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);
exit();
But it only works with local files, I think HTTP_RANGE does not work, it returns the following error in the console
''Failed to load resource: the server responded with a status of 416 (Requested Range Not Satisfiable)''
I need to read Amazon S3 videos, for example: video here
Does anyone have any ideas?
Have you read Document Manual Amazon SDK..?
(1) Download latest stable version of SDK
(2) Extract the .zip file & place in wamp/www folder
(3) Rename config-sample.inc.php file to config.inc.php
(4) Add the access key & secret key (retrieved from Amazon S3 account) into
above file, save & exit
(5) create a sample file to display public / private objects from Amazon S3
thank you Yusnur Hidayah
But I still have problems,
Opening the file by the SDK / StreamWrapper, it takes about 18 seconds for the video to start, see
http://192.241.159.176/file.php
This way it is impossible to make available to users
Usually opens in 1 or 2 seconds, see
https://s3-sa-east-1.amazonaws.com/onlytestes/video.mp4

How to make live streaming using php?

I have live-streaming link where its format extension is .m3u8.
and I want it to be live in to my page.
I tried this code but it does'nt work
<?php
$file = 'http://93.87.85.70/PLTV/88888888/224/3221226661/04.m3u8';
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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 ($range == '-') {
$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);
exit();
?>
Is there something wrong or I am needed to add something there?
Or if this code is wrong what another method am gonna use.
.m3u8 is a playlist file, not an MP4 video. It's commonly used with HLS streams.
An HLS stream is made up of a whole collection of files. There will be audio/video file segments every several seconds, possibly at several bitrates, with the playlist. The playlist is updated regularly.
Even if you did want to proxy these files, your script is not the way to do it. Best to leave it up to the web server (Nginx, Apache, whatever you're using), as that's what it does best. Your script isn't respecting the upstream content type, headers, etc. It's also ignoring any errors on fopen(). I wouldn't use fopen() on a URL anyway... the wrappers aren't always enabled, and then you don't have access to the real status codes and headers. And finally, you've hardcoded the upstream path so your users would only get the playlist and not the media files.
All you have to do is use a client-side player than handles HLS, like JWPlayer or similar. This has nothing to do with PHP. It's done with JavaScript and the Media Source Extensions API.

PHP Stream to html5 Video, Page Navigation not working

i have troubles with Streaming .mp4 Videos from PHP to a HTML5 Video Tag. The Video Streams fine and also moving forward and backward works. BUT: If i click on any Link in my Menu, the Site will not change. The loading Icon Appears on the Tab and the Dev Tools show that there is a request. But the request "waits" until the Video Stream End. So if the Video ended, the new Page will load, but not before.
Any ideas on this?
PS: Adding session_write_close(); before streaming the File solves the Problem. But it looks a bit too hacky for me...
<video style="width:100%;" preload="metadata" controls="">
<source src="/uploads/getfile?image_path=5%2FOKzAAFlSub-VLsnFWvkPWXBLluwOV-Q5DIuqJkPpDubahlAosK.mp4&type=20" type="video/mp4">
Your browser does not support the video tag.
</video>
PHP Code:
$file = Yii::getAlias('#app') . '/../files/uploads/' .$image_path;
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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 ($range == '-') {
$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);
ob_flush();
}
fclose($fp);
exit();
Adding session_write_close(); before streaming the File solves the Problem. But it looks a bit too hacky for me...
There isn’t much “hacky” about this.
A long-running script that keeps a session open will block access to that same session for all other scripts that get started later on (as with your menu link click.)
To avoid that, you close the session in the long-running script as soon as you are done with it, so that the lock on the session data file can be released.
What is quite “hacky” though, is streaming video data via a script in the first place. That is what you should avoid doing in the first place, if at all possible. It is not a good idea in terms of memory usage and script runtimes.
It is better to use already exist PHP package to support partial download. Here is one of them from pear:
http://pear.php.net/manual/en/package.http.http-download.php
It support several types of download (partial, stream , ..). Here is a sample code:
$dl = &new HTTP_Download();
$dl->setData($data);
$dl->setLastModified($unix_timestamp);
$dl->setContentType('application/x-gzip');
$dl->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, 'latest.tgz');
$dl->send();

Streaming an mp4 through a php file progressively

I'm working on a more secure streaming method for our video player. Because each file requires special token authentication and also only allows each token to be loaded once, I'm running MP4 through a php container. This works perfectly inside a HTML5 video tag and prevents users from easily downloading the source.
I'm adapting some code from here
<?php
include('...'); //include site functions
/*
Here I connect to the database, and retrieve the
location of the video which is returned as
$video = "http://domain.tld/folder/file.mp4"
This has been removed for this SO example.
*/
$file = $video;
$fp = #fopen($file, 'rb');
$head = array_change_key_case(get_headers($file, TRUE));
$size = $head['content-length'];
//$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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 ($range == '-') {
$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);
exit();
?>
Problems now arise as I'd like to be able to seek into the video. Skipping to an area in the video which has not been loaded crashes the <video> tag (and chrome).
I'd assume that since the HTML5 player is seeking in seconds and the PHP is loading in bytes, that this cannot be done. Which is why I'm asking here.
What can I do (if anything) to allow progressive streaming?
Is there a more appropriate container format I could use which would serve the same purpose?
In addition to what dlopez said, I recommend to use 3rd-party solution for progressive download with seeking capabilities (AKA pseudo-streaming). You may take a look at the PD solutions listed in Wikipedia: https://en.wikipedia.org/wiki/Progressive_download
Most of them can also prevent video hotlinking protection as well.
I think that you have a conceptual failure. You are treating the mp4 file like if it were a "raw data file". I try to explain myself.
Imagine that you have a text file, and you want to get the chars from position X. You can open the file, point the cursor to the correct position and then read byte by byte your text. This will work fine.
But now image that you want to do the same but with a text processor file. Would you expect the same results? No, because you have a lot of metadata in the file that prevents you from doing that.
Your problem is basically the same. You need to take in consideration the format of the file, managing the file with libraries designed for it, or doing by yourself.
The other option will be to work with raw data files, but in the case of video files, these are going to be really big files.
I hope that I helped you.
I ran into a similar problem. Using stream_get_content($fp, $start) instead of fseek fixed the issue. I was able to get the video to stream fine on all browsers, and seeking (or skipping) throughout the video worked without a problem.
Fully Functional Code
$file = "z.mp4";
$length= filesize($file);
$offset = 0;
$f = fopen($file, 'r');
stream_get_contents($f, $offset);
$pos = 0;
while($pos < $length){
$chunk = min($length-$pos, 1024*8);
echo fread($f, $chunk);
flush();
ob_flush();
$pos += $chunk;
}
You can't use fseek(5654) if you are using file path with http://example.com...... You should use c:/file/abc.mp4 instead. It may solve your problem...

Using php to output an mp4 video

Ok basically I have a project that requires that videos are hidden from the users while still able to see them (by using php). here's what i got so far:
The video.php file has this:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'path/to/movie.mp4');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$out = curl_exec($ch);
curl_close($ch);
header('Content-type: video/mp4');
header('Content-type: video/mpeg');
header('Content-disposition: inline');
header("Content-Transfer-Encoding:­ binary");
header("Content-Length: ".filesize($out));
echo $out;
exit();
?>
and the html file that is supposed to display this is using html5 as it would expect. now here's the thing.. when I straight embed this (not ) it works. but it doesn't work on my iPhone and doesn't work in the tag... if I use the direct file instead of the php wrapper, everything works fine, on my iPhone too...
so I guess my question for this one is this: what are the proper header() information to perfectly replicate an mp4 that can be streamed via iPhone and HMTL5?
Solution derived from: http://mobiforge.com/developing/story/content-delivery-mobile-devices
video.php file:
<?php
$file = 'path/to/videofile.mp4';
$fp = #fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
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 ($range == '-') {
$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);
exit();
?>
Iphones use something called byte-ranges for audio and video requests. See this link for a solution. It's in Appendix A.
http://mobiforge.com/developing/story/content-delivery-mobile-devices
Here is a code snippet that will do what you want (from this question). The PHP solution seems more elegant, and it adds a more efficient solution that might work that uses the web server to serve the content.
<?php
$path = 'file.mp4';
$size=filesize($path);
$fm=#fopen($path,'rb');
if(!$fm) {
// You can also redirect here
header ("HTTP/1.0 404 Not Found");
die();
}
$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: video/mp4");
header('Accept-Ranges: bytes');
header('Content-Length:'.($end-$begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
$cur=$begin;
fseek($fm,$begin,0);
while(!feof($fm)&&$cur<$end&&(connection_status()==0))
{ print fread($fm,min(1024*16,$end-$cur));
$cur+=1024*16;
usleep(1000);
}
die();
More Performance
Note that this is not the most efficient way to do it, because the whole file needs to go through PHP, so you will just need to try how it goes for you.
Assuming the reason you want to do this is to restrict access, and you need more efficiency later, you can use a flag for the web server.
Apache with X-Sendfile module or lightty (nginx info here)
$path = 'file.mp4';
header("X-Sendfile: $path");
die();
This is a bit more advanced and you should only use it if you need it, but it is relaxing to know you have an upgrade option when you start out with something that is rather easy but has mediocre performance.
This code was very handy for me, but I ran into trouble because I am using session vars, and PHP queues access to sessions. If a video was loading, all AJAX requests were impossible, etc. So make sure to call session_write_close() before you start output.
Yes, its easy to do. No need to set those headers manually. Let the server do it automatically.
Heres a working script -
ob_start();
if( isset($_SERVER['HTTP_RANGE']) )
$opts['http']['header']="Range: ".$_SERVER['HTTP_RANGE'];
$opts['http']['method']= "HEAD";
$conh=stream_context_create($opts);
$opts['http']['method']= "GET";
$cong= stream_context_create($opts);
$out[]= file_get_contents($real_file_location_path_or_url,false,$conh);
$out[]= $http_response_header;
ob_end_clean();
array_map("header",$http_response_header);
readfile($real_file_location_path_or_url,false,$cong);

Categories