I am not a specialist in HTML and PHP, but I need to implement the following task: add the ability to download mp3 files directly to the site (without playback through the browser). Found a way to use php-file. The problem is that the larger the file size, the longer the wait to start downloading; and if the file size is> 200 MB, a php error occurs. How can I fix this problem? Maybe there are more suitable options besides using a php file ... Thank you.

HTML:

<a href="d.php?file=http://remoteserver/1.mp3">Скачать</a> 

d.php:

 <?php $file = $_GET['file']; $file = str_replace(' ', '%20', $file); $ch = curl_init($file); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); $output = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status == 200) { 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: ' . strlen($output)); echo $output; die(); } ?> 

  • Tried to overlook in the direction of file proxying through NGINX? - Dmitry Miroshnichenko

1 answer 1

This problem can be solved in HTML by adding the download attribute:

  <a href="http://remoteserver/1.mp3" download>Скачать</a> 

Support this attribute

  • this attribute does not help with mp3 files: most browsers will play the file anyway. - IlinDV10
  • I looked at the dock for this attribute, it only works, the file and the site have the same origin or Content-Disposition: download :( - Dmitry Miroshnichenko
  • and how to implement it? - IlinDV10
  • I would suggest trying to look in the direction of proxying through Nginx, although I can’t give an exact answer. docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy - Dmitry Miroshnichenko
  • In the evening I will experiment if I can write how to do it. - Dmitry Miroshnichenko