How to configure the specified save file name for file download in nginx

PHPz
Release: 2023-06-02 16:04:06
forward
1651 people have browsed it

Generally after we upload the resource file, in order to avoid file name conflicts, the file name will be changed to a meaningless period of characters. This character may be generated by md5, or a string generated by other methods. At this time, when downloading, the file name saved by default will be this meaningless file name (Figure 1).

How to configure the specified save file name for file download in nginx

In fact, nginx supports renaming during downloading, making the file name more friendly.

How to configure the specified save file name for file download in nginx

nginx is also easy to configure: just add the following lines:

Copy code The code is as follows:

location ~* . *\.(doc|txt|jar|zip|apk)(\?.*)?$
{
If ($request_uri ~* ^.*\/(.*)\.(doc|txt |jar|zip|apk)(\?n=([^&] ))$) {
                  add_header content-disposition "attachment;filename=$arg_n.$2";
                                                                                                                                                     
expires 30d;
break;
}

The if line is more critical, so configure it to ensure that when the n parameter is not passed, it will be saved with the original name. No empty names will appear.

Because it is specified in the header form, CDN will save this header information, and it will behave the same when the content is distributed.


The above is the detailed content of How to configure the specified save file name for file download in nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!