我正在亚马逊云硬盘上存储我的文件。还有一个选项可以将response-content-disposition
设置为直接从亚马逊链接。
来自API:
response-content-disposition
:(可选)如果指定,响应的内容处理将设置为该值
我需要设置它下载mp3文件。我不需要在浏览器上播放。
我想设置下载的文件名:Эльбрус Джанмирзоев - Кавказская любовь.mp3
我尝试了很多方法。。。URL代码。。。URL代码+http构建查询等…
但现在的结果是:下载文件时Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3
。
这是我的php代码:
<?php
function file_generate_download_link($filename = false, $force = true)
{
$json['tempLink'] = 'https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb';
$url = $json['tempLink'];
if($force)
{
$data['download'] = 'true';
}
if($filename)
{
$data['response-content-disposition'] = ' attachment; filename*=UTF-8'''.urlencode($filename).'';
}
if(isset($data)) {
$data = http_build_query($data);
$url .= '?' . $data;
}
return $url;
}
echo file_generate_download_link($filename = 'Эльбрус Джанмирзоев - Кавказская любовь.mp3');
?>
这个代码返回给我这个链接:
https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb?download=true&响应内容处置=+附件%3B+文件名%2A%3DUF-8%27%27%25D0%25AD%25D0%25BB%25D1%258C%25D0%25B1%25D1%2580%25D1%2583%25D1%5581%2BD%25D0%2594%25D0%25B6%25DO%25B0%25DO25BD%25DO%25BC%25DO/25B8%25D1%25D0%25DO%25BE%25D0%25B5%25D0%25B2%2B-%2B%25D0%259A%25D0%25B0%25D0%25B2%25D0%25BA%25D0%25B0%25D0%25B7%25D1%2581%25D0%25BA%25DO%25B0%25D1%258F%2B%25DO%25BB%25D1%258E%25D0%25 B1%25D0%25BE%25D0%25B2%25D1%258C.mp3
如果我输入这个链接,Chrome会用这个名称保存这个文件:
Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3
但我需要保存这个名称的文件:
Эльбрус Джанмирзоев - Кавказская любовь.mp3
我的错误在哪里?
明白了!我需要使用rawurlencode函数!
所以正确的代码是:
<?php
function file_generate_download_link($filename = false, $force = true)
{
$json['tempLink'] = 'https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb';
$url = $json['tempLink'];
if($force)
{
$data['download'] = 'true';
}
if($filename)
{
$data['response-content-disposition'] = ' attachment; filename*=UTF-8'''.rawurlencode($filename).'';
}
if(isset($data)) {
$data = http_build_query($data);
$url .= '?' . $data;
}
return $url;
}
echo file_generate_download_link($filename = 'Эльбрус Джанмирзоев - Кавказская любовь.mp3');
?>