开发者

Renaming files when downloading it

I want to rename file when user downloads it.

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

But there is 3 problems with this method:

  1. If I'm sending big (above 3-4Gb) files this way, then my PHP script runs too much time and may be killed by timeout.
  2. If user cancels the download, PHP script continue to read and se开发者_开发技巧nd the file
  3. If user pauses the download, he cannot resume it later.

Is there any nicer way to rename files on download?


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>

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!).

Note: this won't affect anything on your server.


<?php
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?> 
  1. The file will be offered for download as "downloaded.pdf" while its original name was "original.pdf".

Pseudo code

while(1) {
    Echo "..."; //<-- send this to the client
    if (connection_status()!=0){
    die;
    }
}
  1. You can stop the PHP script if a user cancels the browser by using connection_status()

  2. HTTP can not reconnect a closed connection. FTP can!


For the third point, you could try using the HTTP Range headers.
If the user has a resume-capable download client, it should send those headers to your script, which you could use to pinpoint exactly which parts of the file to send.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜