Asked  7 Months ago    Answers:  5   Viewed   34 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, March 31, 2021
answered 7 Months ago

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

Saturday, May 29, 2021
answered 5 Months ago

It would seem I need xdebug.remote_connect_back because I don't have a static IP. I'll post again after I have them add it and it doesn't fix it.

Saturday, May 29, 2021
answered 5 Months ago

If you can rely on file timestamp, use file mask with time-constaint.

See WinSCP time based file download.

If you have to rely on a file name, use the %TIMESTAMP% syntax:

get filename_%TIMESTAMP#yyyymmdd%.txt

Also consider using WinSCP .NET assembly, instead of scripting. Particularly, if you know, say, PowerShell.

Sunday, August 15, 2021
Kaj Lindberg
answered 2 Months ago

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.

Sunday, October 3, 2021
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :