View Full Version : Forcing movies to download
Is there something I can do with htaccess or something to force a movie to download instead of being viewed in the browser or being streamed to a player?
I believe to do this you need to alter the content type of the file being served which requires a handler of sorts to do this which can then be hidden with an .htaccess. It will use more cpu than letting apache serve the file.
<?php
header("Content-Type: video/x-ms-wmv");
header('Content-Disposition: attachment; filename="test.wmv"');
$file=fopen("test.wmv","rb");
fpassthru($file);
close($file);
?>
properly wrapped with a mod_rewrite, you could have it automatically parse and hand out files. Let me know and I can come up with something a little closer to what you want.
It's for inside of member's area. Right now the members are told to right mouse click but this seems too complicated for some of them based on help emails.
The mp4 movies can be viewed in the browser but then there are also links to download the mp4 movies plus the m4v and wmv file.
Screen shot
I can change around how things are organized but this is how it is being done at the three sites that I manage.
One pic per post so here is the shot of the directory
.htaccess
RewriteEngine on
RewriteRule vids/(.*) /video.php?video=$1 [L]
video.php
<?php
$filename = "/var/www/domain.com/" . $_REQUEST['video'];
if ( (preg_match("/[a-z0-9\-_]+.(mp4|wmv|mp4a)/i",$_REQUEST['video'])) &&
(file_exists($filename)) ) {
header("Content-Type: " . mime_content_type($filename));
header('Content-Disposition: attachment; filename="' . $_REQUEST['video'] . '"');
$file=fopen($filename,"rb");
fpassthru($file);
close($file);
} else {
print "error retrieving file";
}
?>
Not a lot of error checking in it, file paths will need to be changed, but, this is probably the basics of what you're looking for. Mimetype detection will help, you can fix the preg_match to match the extensions you want to allow. filenames cannot contain more than one . and must be comprised of all letters, numbers, - and _.
That does look exactly like what I'm looking for. :)
Lets see if I've got this right...
I link to the video.php and the video.php calls up the movie that is specified in its server path and if I have four movies then I make four copies of video.php
I should change this to this for the 3 movie extensions that I use.
(mp4|wmv|m4v)
Each directory would have its own set of video.php files and each video.php would have the server path to that movie.
I'll try it out tomorrow on the Ana site.
vBulletin® v3.8.1, Copyright ©2000-2026, Jelsoft Enterprises Ltd.