Viewed   73 times

I'm 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 on a separate server.

Any way I can force the download in PHP?



You could try something like this:

$file_name = 'file.avi';
$file_url = '' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename="".$file_name."""); 

I just tested it and it works for me.

Please note that for readfile to be able to read a remote url, you need to have your fopen_wrappers enabled.

Wednesday, September 14, 2022
$ch = curl_init();
$source = "";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "/asubfolder/";
$file = fopen($destination, "w+");
fputs($file, $data);
Thursday, November 3, 2022

You can create a run of the mill curl script that connects to a remote server and fetches data, inserts into your db, set limits and exceptions. Then create an entry in crontab to run this script every 10 minutes. I have a similar setup running for one of my website which fetches data from stock exchange and updates a local cache, another script has the task of consuming the cache as and when required.

Saturday, December 17, 2022

There are some ideas over in this thread. I don't know if the readfile() method will save memory, but it sounds promising.

Sunday, September 25, 2022

Try finfo_file(). You have to call it passing the filepath. Example:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['control_name_from_client']['tmp_name']);

You need the Fileinfo extension. As PHP manual says:

The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach the heuristics used do a very good job.

Monday, November 21, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :