Rename and download mp3 file

2019-06-05 23:53发布

问题:

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?

回答1:

http://ie2.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



回答2:

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.



标签: php file rename