Rename and download mp3 file

2019-06-05 23:24发布

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?

标签: php file rename
2条回答
Melony?
2楼-- · 2019-06-05 23:48

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.

查看更多
3楼-- · 2019-06-05 23:56

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

查看更多
登录 后发表回答