0

how can i force for download mp4 file from url?

url = "https://clips-media-assets2.twitch.tv/AT-cm%7C31747331920-offset-17408-720.mp4"

i tried this code but its not working

header('Content-Type: video/mp4');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\"");
echo file_get_contents($links); 
Dreamenemy
  • 95
  • 1
  • 9
  • 2
    I think `video/mp4` would default to the browsers player, try `application/octet-stream`. https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php – user3783243 Dec 23 '18 at 16:17

1 Answers1

0

You have to be careful if you use external link in you server. Use this code only for your server file.

<?php
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary'); 
header('Content-disposition: attachment; filename="video.mp4"'); 
readfile('/video/offset-17408-720.mp4'); 
user2226755
  • 12,494
  • 5
  • 50
  • 73
  • It seems that my problem is with readfile, because although this script works, weather or not the client closes their browser, readfile reads the entire contents of the mp4, setting up the possibility of abuse with scripts initiating the download and immediately closing the progress. – Chewie The Chorkie Apr 21 '22 at 16:04