下载文件时重命名

下载文件时重命名

问题描述:

我想在用户下载文件时重命名.

I want to rename file when user downloads it.

现在,我正在发送content-disposition和content-length标头,然后使用fpassthru PHP函数将文件发送给用户.

Right now, I'm sending content-disposition and content-length headers and then send file to user with fpassthru PHP function.

但是此方法存在3个问题:

But there is 3 problems with this method:

  1. 如果我以这种方式发送较大的文件(3-4Gb以上),则我的PHP脚本运行了太多时间,并可能因超时而终止.
  2. 如果用户取消下载,PHP脚本将继续读取并发送文件
  3. 如果用户暂停下载,他以后将无法继续下载.

是否有更好的方法来重命名下载的文件?

Is there any nicer way to rename files on download?

自从提出问题以来,已经过去了一段时间,并且html5为我们提供了另一种方法:

Since the question was asked, some time has gone by and html5 gave us a different approach to this:

<a href="someWeiredFileName.rar" download="coolFilename.rar">Download</a>

attribute 下载(学习更多)定义了默认的filename,用于将文件存储在客户端计算机上(他仍然可以更改它!).

The attribute download (learn more) defines the default filename for storing the file on the clients computer (he'll still be able to change it!).

注意:这不会影响您服务器上的任何内容.