I am trying to output an mp4 video file through PHP.
When it is used through a flash player (eg. flowplayer) it is working great.
But when I'm trying to use it as a source on an html5 video tag or to call directly the php file, it doesn't work.
The code I use is the following:
$filesize = filesize($file);
header("Content-Type: video/mp4");
if ( empty($_SERVER['HTTP_RANGE']) )
{
header("Content-Length: $filesize");
readfile($file);
}
else //violes rfc2616, which requires ignoring the header if it's invalid
{
rangeDownload($file);
}
and rangeDownload function is from http://mobiforge.com/developing/story/content-delivery-mobile-devices Appendix A.
Even when I use a Content-Range header (Content-Range:bytes 0-31596111/31596112), it stucks on downloading 30.13 MB of the video.
Finally I've found a way to make it work
header("Content-Type: $mediatype");
if ( empty($_SERVER['HTTP_RANGE']) )
{
header("Content-Length: $filesize");
$fh = fopen($file, "rb") or die("Could not open file: " .$file);
# output file
while(!feof($fh))
{
# output file without bandwidth limiting
echo fread($fh, $filesize);
}
fclose($fh);
}
else //violes rfc2616, which requires ignoring the header if it's invalid
{
rangeDownload($file);
}
It is working in direct link of the php file and inside html5 video tag.
But in order to work in Flowplayer (and maybe in other flash/html5 players) you need to add a mp4 extension (eg. view.php?id=XXX&file=type.mp4)
This could have to do with your browser and what plugin it uses to view video files ie) quicktime. The reason it works with Flash is flash handles buffering and time sync and such. It is usually not recommended to let the browser handle playing media files, because it completely depends on the browser configuration and the plugins they have installed.
Some browsers automatically download media files, it's completely configurable by browser and end user.
Related
$file_name = $_GET['title'];
$file_url = $_GET['url'] . $file_name;
header('Content-Type: video/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
I'm using this code to download files in my site fetching from another websites.
It works if my url looks like:-
https://www.example.com/video_download.php?title=video.mp4&url=http://googlevideo.com/video/download/223689/289048
(example)
So, it starts downloading by fetching the video file from http://www.googlevideo.com/video/play/221589 to my site.
But my problem is that the file can be accessed if the person uses a PC.
Can I change the User Agent by using header()?
Is it possible?
So if I change the user agent into a PC user agent, so it can be downloaded from a mobile!
I'm sorry, but the User Agent has nothing to do with readfile() function. Readfile() will just throw the raw file input into your browser. Useful for e.g. rendering images through PHP to the client without having to expose the real file name.
Indeed, it is possible to render video to the client with readfile(), but using a HTML5 video tag will dramatically improve performance. This will also provide better mobile support.
Hope this helps you,
You can use stream_compy_to_stream
$video = fopen($file_url);
$file = fopen('videos/' . $title . '.mp4', 'w');
stream_copy_to_stream($video, $file); //copy it to the file
fclose($video);
fclose($file);
I wrote a class for downloading youtube video files. you can find it here.
I'm sure this is a simple task, but on my wordpress site I want to create a download button that forces an .mp3 download, without opening a player (when left clicked), or the user having to right-click 'save target as'. I just need a straight forward button, that when left-clicked causes a file to be downloaded (as well as being easily trackable by Google Analytics).
Is a .php script required for this? You'd think this would be a very common function, and easy to solve....but I have spent hours on this and have been unable to get anything to work.
*if it's not obvious my coding skills are nearly non-existent.
I really appreciate anybody's time who can help me figure this out. Thanks!
***EDIT
Just found this on another post, but no comments if it would work or not. It was for a .pdf file though...
<?php
if (isset($_GET['file'])) {
$file = $_GET['file'] ;
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
?>
Save the above as download.php
Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.
You can use it like so:
Add the following link to your HTML file.
Download the cool PDF.
Well, this is possible, but you need to write a script to do it. This is a pretty poor (security and basic coding wise) from http://youngdigitalgroup.com.au/tutorial-force-download-mp3-file-streaming/
file: downloadit.php
<?php
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header ("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
you would then place it into a publicly accessible folder and build your links as such:
http://www.yoursite.com/downloadit.php?file=/uploads/dir/file.mp3
what this does is tells the browser to treat the file as a stream of bytes, rather than a particular MIME type which the browser would ordinarily do based on the file extension.
I'm trying to feed an mp4 file to flash player via php and the video is downloaded completely before starting playback.
$src = '/var/www/user/data/www/domain.com/video.mp4';
if(file_exists($src) and is_readable($src)) {
header('Content-Type: video/mp4');
header('Content-Length: '.filesize($src));
readfile($src);
} else die('error');
I've tried curl with similar results. Any ideas what's causing this delay?
Most likely your Flash player is hoping you'll handle HTTP Range requests so it can get started faster on the playback.
The HTML5/Flash audio player jPlayer has a section in their developer guide about this. Scroll to the part about Byte-Range Requests:
Your server must enable Range requests. This is easy to check for by
seeing if your server's response includes the Accept-Ranges in its
header.
Also note that they offer a PHP solution for handling Range requests if you have to use PHP instead of a direct download.
smartReadFile.php
https://groups.google.com/forum/#!msg/jplayer/nSM2UmnSKKA/bC-l3k0pCPMJ
Another option would be to just have apache send the file it self as opposed to reading it in php and dumping it to the output using X-Sendfile.
First make sure apache is compiled with sendfile support then alter your output code to be:
header ('X-Sendfile: ' . $src);
header ('Content-Type: video/mp4');
header ('Content-Disposition: attachment; filename="' . $filename . '"');
exit;
This is normally faster than doing it via PHP.
I have uploaded some sample mp3 files to a directory outside of httpdocs, I have ensured that this is accessible to PHP by configuring open_basedir correctly and tested that this directory is working.
What I would like to do is stream these files via a PHP file as non-authenticated users should never have access to these files. I am currently using jPlayer and expect the setMedia function should look similar to this:
$("#jquery_jplayer").jPlayer("setMedia", { mp3: "stream.php?track=" + id + ".mp3" });
I have tried setting content headers etc in stream.php and it currently looks like this:
$filePath = "../song_files/mp3/";
$fileName = "$_GET[track].mp3";
header("Content-Type: audio/mpeg");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
getFile($filePath + $fileName);
If I load this page directly, the mp3 file downloads and plays fine, but when I use the above javascript, jPlayer doesn't play the track.
I have had a look at this post ( Streaming an MP3 on stdout to Jplayer using PHP ) and it appears the user was trying to achieve exactly what I want, but upon testing the solution I keep running into a problem, all I get is "CURL Failed".
Are there any different methods I can use to achieve this. Pointing me in the right direction would be greatly appreciated.
After searching around some more I have found a solution that is working fine. I used the code from a similar topic ( PHP to protect PDF and DOC )
I will place the code I used here to help answer the question correctly:
//check users is loged in and valid for download if not redirect them out
// YOU NEED TO ADD CODE HERE FOR THAT CHECK
// array of support file types for download script and there mimetype
$mimeTypes = array(
'doc' => 'application/msword',
'pdf' => 'application/pdf',
);
// set the file here (best of using a $_GET[])
$file = "../documents/file.doc";
// gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);
// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);
// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
// reads file and send the raw code to browser
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
// closes file after whe have finished reading it
fclose($fp);
</code></pre>
I am trying to get my php video handler file to serve up videos and am testing it on the file handler page itself without luck.
Weird behaviour: A window just pops up asking me if I want to download the file, even if I delete the readfile(). If I change video/x-flv to video/flv then a player loads in my window but the file does not play. Also if I take away that header all together my browser crashes.
I figured this script should place the video in the browser at the least and have it be playable in the browser if I am testing the file directly with the browser. The file path is correct after the query... Also the file is outside of my web directory but I don't think that should matter because I can serve images outside the directory successfully using a similar script. Anyone have any ideas?
$sql="SELECT file_name FROM video WHERE vid_id=?";
$stmt=$conn->prepare($sql);
$result=$stmt->execute(array($ID));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$file_name = $row['file_name'].".flv";
}
$path="/home/g/Desktop/processed/".$file_name."";
//check if image is readible and type
if (is_readable($path)) {
header('Content-Length: '.filesize($path)); // provide file size
header("Expires: -1");
header('Content-Type: video/x-flv');
$content=readfile($path);
}
else {
error_log("Can't serve video: $file_name");
}
Streaming doesn't work like that.
To have a proper streaming you need to use a Player like: http://flowplayer.org/
If you just send the content of the video, the browser will pop up the save dialog
I was unable to use php to pass videos to OS player. I switched to flow player and things are working.