Im looking to add a "Download this File" function below every video on one of my sites. I need to force the user to download the file, instead of just linking to it, since that begins playing a file in the browser sometimes. The problem is, the video files are stored in a remote server.
Any way I can force the download in php?
-
You could try something like this:
$file_name = 'file.avi'; $file_url = 'http://www.myremoteserver.com/' . $file_name; header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit;I just tested it and it works for me.
Please note that for
readfileto be able to read a remote url, you need to have yourfopen_wrappersenabled.Chris KL : Just remember to go: str_replace('"', '\\"', $file_name) Before putting it in the headerZoredache : The flaw with this is that all the video would run through his server. If the video is on a separate server for bandwidth reasons, this solution will not be acceptable.Paolo Bergantino : Point taken. From the sound of the question, though, I got the impression that he does not have access to this remote server, otherwise it would be a trivial problem. If that is the case, as far as I know this would be the only solution.Yegor : I do have access to the remote server.jmoz : This works for me but it does not tell you the file size and estimated time left, is there a header you need to specify to do this? Also, is it possible to use the filename of the file as it was meant to be, not to re-set it?zidane : I have same problem, and access to remote file server? How to initiate download form app server and serve file from remote, without using bandwith of an app server? -
just some random ideas:
- let your app download the file, zip it and then serve it to the client
- feature your own download manager like a java app or so
- maybe just send the correct headers to disable streaming?
PoweRoy : eek, a custom download manager. People won't be happy to see a slow java applet running to pass a file. Instead use the other comment, like Paolo Bergantino -
Try this:
<?php $FileName = '/var/ww/file.txt'; header('Content-disposition: attachment; filename="'.$FileName.'"'); readfile($FileName);The key is the
header(). You need to send the header along with the download and it will force the "Save File" dialog in the user's browser. -
<?php $FileName = '/var/ww/file.txt'; header('Content-disposition: attachment; filename="'.$FileName.'"'); readfile($FileName);using this code. is it possible to save the file name to what you want. for example you have url: http://remoteserver.com/file.mp3 instead of "file.mp3" can you use this script to download the file as "newname.mp3"
0 comments:
Post a Comment