There are 2 folders on the server:

My server/ -public_html/ --index.php --about.php --shop.php -video/ --film.mp4 

The domain site.com leads to the public_html folder. How to make it so that when you click on site.com/about.php, film.mp4 starts downloading ?

  • to transfer film.mp4 to public_html is not an option? - Jurij Jazdanov
  • @JurijJazdanov is not an option. the customer requires just such a structure. - Sergey

2 answers 2

 if (file_exists('video/'.$file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($file).'"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); exit; } 

Insert at the beginning of the file about.php

  • Will the result be a file download? - Sergey
  • I gave you a template, there you can guess that the $ file variable is film.mp4, the other parameters are by default. - Orest Ganulyak February
  • thank you ....... - Sergey

those. Do I need to open the page and start downloading? Then through js

 window.open(url, '_blank'); 

where url is one of two things:

1) Link to the film.mp4 file, but downloading will not always start, it may open in the built-in player

2) Link to php force download script, something like this

  if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } // get the file mime type using the file extension switch(strtolower(substr(strrchr($file_name, '.'), 1))) { case 'pdf': $mime = 'application/pdf'; break; case 'zip': $mime = 'application/zip'; break; case 'jpeg': case 'jpg': $mime = 'image/jpg'; break; default: $mime = 'application/force-download'; } header('Pragma: public'); // required header('Expires: 0'); // no cache header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Last-Modified: '.gmdate ('D, d MYH:i:s', filemtime ($file_name)).' GMT'); header('Cache-Control: private',false); header('Content-Type: '.$mime); header('Content-Disposition: attachment; filename="'.basename($file_name).'"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: '.filesize($file_name)); // provide file size header('Connection: close'); readfile($file_name); // push it out exit(); 
  • It says that when you go to site.com/about.php, the download starts as I understand it, that is, js is too much - Orest Ganulyak
  • No, you just need to start downloading. - Sergey