Hello! There are, say, 2 servers, the site itself is on the 1st server, and the second is music stored, but on the second server I cannot edit the .htaccess file. Let me explain why you need to edit this file: files with mime-type mpeg / mp3 are output from the 2nd server. Because of this, when I put direct links to the site, the music is played by the browser instead of downloading. The main question is how to make music download. Write any method, varint, if not difficult with examples.
- You can try to first download the file on the client using javascript, and then offer to save it. - Vladimir Gamalyan
|
1 answer
function download($URL, $filename) { header('Content-Type: application/octet-stream'); header("Content-Disposition: attachment; filename=\"{$filename}\""); $fileHeader = fopen($URL, 'r', false); fpassthru($fileHeader); fclose($fileHeader); } download('https://cs9-5v4.vk.me/p12/4986cda949df08.mp3', 'Вариант1.mp3'); If it doesn't work. Check the PHP allow_url_fopen
Option number 2. Less acceptable
function download($URL, $filename){ $data = base64_encode(file_get_contents($URL, FILE_BINARY)); header('Content-Type: application/octet-stream'); header('Content-Length: ' . strlen($data)); header("Content-Disposition: attachment; filename=\"{$filename}\""); $stream = fopen('data:text/plain;base64,' .$data, 'r'); fpassthru($stream); fclose($stream); } download('https://psv4.vk.me/c611317/u55640938/audios/f186a32dd8d4.mp3','Вариант2.mp3'); |