tags:

views:

85

answers:

2

Hello. 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 send the file
  3. If user pauses the download, he cannot resume it later.

Is there any nicer way to rename files on download?

+1  A: 
<?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 scipt if a user cancels the browser by using connection_status()

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

powtac
Great. That's exactly what I do now, only using fpassthru function instead of readfile.But how your solution will help to solve those three problems I pointed in my question?
Boffin
+1  A: 

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.

Atli