Force PHP to download MP3 file from URL - php

I have mp3 file url and I want to make it downloadable for user.
I'm using below code but this is not working.
<?php
$file = 'http://r1---sn-2uja-pncz.googlevideo.com/videoplayback?pl=17&ip=39.33.149.149&ms=au&mt=1460124725&mv=m&id=o-ALL1b-5qDY-vJciSMtIjNoeHW6NSl0a67lvMj_IDTJ_V&mm=31&mn=sn-2uja-pncz&keepalive=yes&clen=3694396&pcm2cms=yes&sparams=clen,dur,gir,id,initcwndbps,ip,ipbits,itag,keepalive,lmt,mime,mm,mn,ms,mv,pcm2cms,pl,source,upn,expire&ipbits=0&initcwndbps=163750&mime=audio/webm&lmt=1404826626593256&source=youtube&dur=290.350&gir=yes&key=yt6&signature=07AF8F194980DA82DA85AC08FFCC99CA8E459336.38D33A3B46AD95087B779B646C8139A85EE55555&fexp=9405969,9416126,9416891,9420452,9422596,9423662,9426926,9427902,9428398,9429294,9429854,9431270,9432651,9433088,9433186,9433425,9433703&sver=3&upn=j6a9XvntKKY&expire=1460146538&itag=171';
// URL is not path of mp3 file but exact mp3 file
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
Any suggestion why this is not working.
UPDATE
I try below code and it give forbidden 403 access.
header('Content-type: application/mp3');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
These codes are working for .mp3 files not not direct url like this $file because there is not .mp3 but url which return mp3 file formate.
mp3 is playing well in this url, Now how can I download it.

Related

how to hide original downloading link of external site using php

so I need your solution.
my site, example.com/test.php when user click on it, it will download file from external site externalsite.com/file.mkv
I just want to mask the external site url, so that when user click on example.com/test.php it will get file from external site url and never show original site url.
I have try below code but not working
<?php
$id = $_GET['id'];
$filename = "https://externalsite.com/20230113100300if_/https%3A%2F%2Fs38.123apps.com%2Fvconv%2Fd%2F63c12a662da0e_mkv_ymfVHPvq.mkv";
header("Content-disposition: attachment; filename=".$filename."");
header('Content-type: application/octet-stream');
readfile($filename);
?>
Change the name in the Content-disposition: header to just the basename, not the full URL.
So replace
header("Content-disposition: attachment; filename=".$filename."");
with
header("Content-disposition: attachment; filename=".basename($filename));

PHP header() Can not open xlsx file after download

I use header() to download an xlsx file from given url. The file is downloaded but I can't not open it. It shows error
Below is my code
$url = "http://example.com/attachment/file.xlsx"
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment; filename=Test.xlsx');
readfile($url);
exit();
Randy, your question looks weird. The URL in the serve response is way different than the one in your code.
Before commencing the download - sending the headers, do a is_file() or other check on the URL and only start the download if the file exists.
I suspect you are trying fopen on URL, not local file and the URL may be either incorrect or on server not allowing fopen on URLs.
Sample:
$url = "http://example.com/attachment/file.xlsx";
if (!fopen($url,'r')) exit('File/URL not accessible');
else fclose($url);
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename=Test.xlsx');
readfile($url);
exit();

how to download a file from file_get_contents?

I want to get a file via file_get_contents to copy and rename it and then trigger the download. In other words, a user click a link to a controller, the controller does the business and then return the new file to download.
All fine, the only thing I can't do till now is rename and force donwload of the file.
You can change the headers and echo out the file:
// Download the file
header('Content-Disposition: attachment; filename="myfile.csv"');
header("Content-Type: text/csv");
header("Content-Length: " . filesize($outputName));
echo (file_get_contents($outputName));
unlink($outputName);

PHP force download, Not working with variables

When
header('Content-disposition: attachment; filename=1330554893-COVER.jpg');
header('Content-type: jpeg');
readfile('watermarked/1330554893-COVER.jpg');
Is run in a file for example "testdownload.php" It downloads the image
"watermarked/1330554893-COVER.jpg"
and names it
"1330554893-COVER.jpg"
But when I try make the code dynamic to download different files.
header("Content-disposition: attachment; filename={$newFileName}");
header("Content-type: jpeg");
readfile("{$findFile}");
where
$newFileName = "1330554893-COVER.jpg" and $findFile = "watermarked/1330554893-COVER.jpg"
It downloads an image "1330554893-COVER.jpg" but it cannot be opened and I get an error "Windows Photo Viewer can't open this picture because eaither Photo Viewer doesn't support this file format"
Thanks for helping :)
Allrite then, don't use readfile(), try echo file_get_contents after the headers!

how to download the video using php script

In my program I want to add a download option to download the currently straming video. I tried this code:
$psp = "Tom_20_amp__20Jerry_20race-1.flv";
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=$psp");
But I get this error:
"C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\Tom_20_amp__20Jerry_20race-1-5.flv" is not a valid FLV file.
the video streaming properly. please guide me
Change you're header from :
header("Content-type:application/octet-stream");
To :
header("Content-type: video/flv");
Then you can do :
header("Content-Disposition:attachment;filename=\"$psp\"");
//allways a good idea to let the browser know how much data to expect
header("Content-length: " . filesize($psp) . "\n\n");
echo file_get_contents($psp); //$psp should contain the full path to the video
If you are looking to allow files to be downloaded instead of streamed then something like this should work. You will obviously need to change the paths.
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
EDIT
In your case it will be something like this.
// We'll be outputting a video
header('Content-type: video/flv');
// It will be called video.flv
header('Content-Disposition: attachment; filename="video.flv"');
// The PDF source is in original.flv
readfile('original.flv');

Categories