Home  >  Article  >  Operation and Maintenance  >  How to configure the specified save file name for file download in nginx

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

PHPz
PHPzforward
2023-06-02 16:04:061653browse

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete