PHP nginx fastdfs Download File Rename

Source: Internet
Author: User
Tags readfile

In fact, Fdfs can pass in a filename parameter, specifying the file name.

M00/00/00/fwaaavgmateaafjtaaaabbw-xbm368.txt?filename=test.txt

This way the user will get the Test.txt file name when downloading.

The following content is for entertainment only

First of all, Fastdfs.

Then install the Nginx and Fastdfs-nginx modules

Configure Nginx.

Detailed tutorials are available online here.

The problem is that all of our downloaded files are fdfs converted file names, which is unfriendly to the user.

For example, a user uploads a novel. txt file through the website and then submits it to Fdfs, and the file name becomes a combination of letters.

When other users download, the resulting files are not novel. txt, very unfriendly.

Here's the solution.

First, you can save the original file name from the Web site database.

Then download, you can use PHP ReadFile () from the real path to read the file, the output time by specifying the head header to change the file name.

However, with ReadFile (), the additional I/O overhead, with a large traffic, can add to the burden on the server.

Here is another way, that is, through the Linux sendfile feature, let Nginx directly from the real address to read the file and send to the user, and can also be renamed.

What needs to be done is to write the following header in the download.php:

Header ("content-disposition:attachment; filename= rename filename. txt ");
header ("Content-type:application/octet-stream");
header (' X-accel-redirect:m00/00/00/fwaaavgmateaafjtaaaabbw-xbm368.txt ');//fdfs file path
header ("X-accel-buffering:yes");
header ("x-accel-limit-rate:102400");//Speed limit byte/s
//header ("Accept-ranges:none");//single thread Limited multithreading


This is OK, when you open download.php in the browser, PHP output These headers, will be captured by Nginx, and then redirected directly to the X-accel-redirect file, sent directly to the user, and the filename header is also in effect.

This not only hides the real file address, but also can rename the file, very good, very powerful!

remark:

in the Nginx configuration file

location/m00/

{

Internal;

root/fdfs_storge/data/;
Ngx_fastdfs_module;

}

If you add internal to this URL, it means that it only allows internal access, and when you download the Fdfs file directly from your browser, you will be prompted with a 404 error. can only be downloaded via download.php.

PHP nginx fastdfs Download File Rename

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.