Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a video file available for download?

Tags:

html

href

I am trying to offer a download option of videos on my site. I have direct links (which have .mp4/.webm ending) available for download (they are not hosted on my server if that matters). This is what I tried:

     <a href="http://stream.flowplayer.org/bauhaus/624x260.webm" download>Download</a>

It only works on chrome, in FireFox it will just open the video on the browser itself.

like image 469
user1938653 Avatar asked Nov 02 '22 20:11

user1938653


2 Answers

You need a wrapper script which sets the Content-Type Content-Disposition headers appropriately, and outputs the file you want to serve.

In PHP this would be done like this:

Filename: 624x260.php

<?php
// We'll be outputting a webm video
header('Content-type: video/webm');

// It will be called downloaded.webm
header('Content-Disposition: attachment; filename="download.webm"');

readfile('624x260.webm');
?>

You would then link to the PHP file instead, as follows:

<a href="624x260.php">Download</a>
like image 98
Alfie Avatar answered Nov 09 '22 09:11

Alfie


if you happen to have an apache server where you can edit the .htaccess file, add this line.

AddType application/octet-stream .webm

If you wish to not do this, you could do this through php as well.

PHP Code:

<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

HTML Code:

<a href="direct_download.php?file=fineline.mp3">Download the mp3</a>
like image 25
Kyle Avatar answered Nov 09 '22 09:11

Kyle