robpal
robpal

Reputation: 834

Rename and download mp3 file

for example i have link :

http://example.com/song.mp3

and when user download it, file will be renamed to

artist-song.mp3

I used this code :

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://example.com/song.mp3");
header('Content-Disposition: attachment; filename="a-s.mp3"');
exit();
?>

But it doesn't work. What do I need to do?

Upvotes: 1

Views: 1245

Answers (2)

Marc B
Marc B

Reputation: 360572

The Location header causes the client browser to request the file from the specified URL. That'll be a completely NEW and SEPARATE http request, with its own content-type/content-disposition headers.

The Content-disposition header you're issuing here will not be honored, as it is from a completely DIFFERENT request.

Upvotes: 0

Kae Verens
Kae Verens

Reputation: 4079

https://www.php.net/readfile

the example in the docs is pretty much what you want.

in particular, see the header('Content-Disposition: attachment; filename='.basename($file)); bit

Upvotes: 4

Related Questions