简体   繁体   English

如何使用 content-disposition 标头设置下载的 UTF-8 文件名?

[英]How to use the content-disposition header to set a downloaded UTF-8 filename?

I am storing my files on Amazon Cloud Drive.我将我的文件存储在 Amazon Cloud Drive 上。 And there are option for setting response-content-disposition to direct links from Amazon.并且可以选择将response-content-disposition为来自亚马逊的直接链接。

From the API:从API:

response-content-disposition : (Optional) If specified, the content-disposition of the response will be set to this value response-content-disposition :(可选)如果指定,则响应的内容处置将设置为此值

I need to set it download mp3 files.我需要设置它下载mp3文件。 I don't need to play it on browser.我不需要在浏览器上播放它。

And I want to set downloaded file name: Эльбрус Джанмирзоев - Кавказская любовь.mp3我想设置下载的文件名: Эльбрус Джанмирзоев - Кавказская любовь.mp3

I tried many ways... Urlencode... Urlencode + http build query etc...我尝试了很多方法... Urlencode... Urlencode + http 构建查询等...

But now the result is: Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3 when downloading files.但现在的结果是:下载文件时, Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3

And here is my php code:这是我的 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');
?>

This code returns me this link:此代码返回此链接:

https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb?download=true&response-content-disposition=+attachment%3B+filename%2A%3DUTF-8%27%27%25D0%25AD%25D0%25BB%25D1%258C%25D0%25B1%25D1%2580%25D1%2583%25D1%2581%2B%25D0%2594%25D0%25B6%25D0%25B0%25D0%25BD%25D0%25BC%25D0%25B8%25D1%2580%25D0%25B7%25D0%25BE%25D0%25B5%25D0%25B2%2B-%2B%25D0%259A%25D0%25B0%25D0%25B2%25D0%25BA%25D0%25B0%25D0%25B7%25D1%2581%25D0%25BA%25D0%25B0%25D1%258F%2B%25D0%25BB%25D1%258E%25D0%25B1%25D0%25BE%25D0%25B2%25D1%258C.mp3 https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb?download=true&response-content-disposition=+attachment%3B+filename%2A%3DUTF-85%D20%DUTF-85%D27D %25BB%25D1%258C%25D0%25B1%25D1%2580%25D1%2583%25D1%2581%2B%25D0%2594%25D0%25B6%25D0%25B0%25D0%25D1%25D0%20%20%B5B5B5B5D0 %2580%25D0%25B7%25D0%25BE%25D0%25B5%25D0%25B2%2B-%2B%25D0%259A%25D0%25B0%25D0%25B2%25D0%25BA%20%20%D720%20%25%D72 2581%25D0%25BA%25D0%25B0%25D1%258F%2B%25D0%25BB%25D1%258E%25D0%25B1%25D0%25BE%25D0%25B2%25D1%258C。

If I enter this link Chrome saves this file with this name:如果我输入此链接,Chrome 会使用以下名称保存此文件:

Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3

But I need excatly save file with this name:但我需要用这个名字保存文件:

Эльбрус Джанмирзоев - Кавказская любовь.mp3

Where is my mistake?我的错误在哪里?

Gotcha!知道了! I need to use rawurlencode function!我需要使用 rawurlencode 函数!

So the right code is:所以正确的代码是:

<?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');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:RFC-2231如何将UTF-8字符串编码为Content-Disposition文件名 - PHP: RFC-2231 How to encode UTF-8 String as Content-Disposition filename 如何更正Content-Disposition标头中的文件名值? - How to correct filename value in Content-Disposition header? PHP Content-Disposition header:文件名中的特殊字符 - PHP Content-Disposition header: Specialcharacters in the filename 如何在Content-Disposition标头中使用带有特殊字符的文件名? - How can I use a filename with special characters in a Content-Disposition header? 如何在Android设备上设置“Content-Disposition:inline;”文件的文件名? - How to set a filename for “Content-Disposition: inline;” file on android device? php标头内容处置 - php header Content-disposition 标头(“ Content-Type:应用程序/ zip”)和标头(“ Content-Disposition:附件;文件名= $ fileName”)在wordpress中不起作用? - header(“Content-Type: application/zip”) and header(“Content-Disposition: attachment; filename=$fileName”) not working in wordpress? PHP - 如何在Content-Disposition中设置完整目录路径? - PHP - How to set full directory path in Content-Disposition? Firefox将下载retrieve.php,而不是每个Content-Disposition标头指定的文件名 - Firefox downloads retrieve.php instead of filename given per Content-Disposition header 客户端缺少内容处理标头 - Content-Disposition header missing at client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM