Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Force Download MP4 Audio/Video Files using PHP or .htaccess

Hi Friends,

For Membership Video Site, client is looking for Force Download feature for MP4 Audio/Video files. This feature is only for their registered users to download the video files directly to their PC. For zip files, it will open and ask to save file automatically. But for MP4 Audio/Video files, case is not the same. If you try to do the same with MP4 Audio or Video files, it will just start playing audio or video in new tab or window. so question is how to Force Download MP4 Audio/Video Files?

If you want to implement force download feature for MP4 Audio/Video files, it is possible in 2 ways. You can use the code depending on your requirement.

  • Force Download MP4 Audio/Video Files using PHP for individual files
  • Force Download MP4 Audio/Video Files using .htaccess for all files

Code for Force Download MP4 Audio/Video Files using PHP

function force_download_file($file) {				
 header( 'Expires: Mon, 1 Apr 1974 05:00:00 GMT' );
 header( 'Pragma: no-cache' );
 header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
 header( 'Content-Description: File Download' );
 header( 'Content-Type: application/octet-stream' );
 header( 'Content-Length: '.filesize( $file ) );
 header( 'Content-Disposition: attachment; filename="'.basename( $file ).'"' );
 header( 'Content-Transfer-Encoding: binary' );
 readfile( $file );  

You can call this function when you click on Download Button and process. It will show you pop up.

Code for Force Download MP4 Audio/Video Files using .htaccess

If you want force download feature for all mp4 audio/video files in your site, you can use below code in .htaccess. Now if anyone click on any link of mp4 audio/video files on your site, it will show force download popup.

<FilesMatch "\.(mp4|MP4)">
ForceType application/octet-stream
Header set Content-Disposition attachment
Thank you!


Leave a Reply

Your email address will not be published.