tags:

views:

127

answers:

4

Hi,

i am using this for sending file to user

header('Content-type:  application/zip');
header('Content-Length: ' . filesize($file));
header("Content-Disposition: attachment; filename="file.zip");
readfile($file);

i want to delete this file after user download it, how can i do this ?

thx

EDIT: my scenario is like that, when user hits download button, my script will create a temp. zip file and user download it then that temp zip file will be delete

EDIT2: OK best way seems running a cron job that will be cleaning temp files once an hour.

EDIT3: I tested my script with unlink , it works unless user cancel the download. if user cancel the download, zip file stays on the server. so that is enough for now :)

EDIT4: WOW! connection_aborted() made the trick !

ignore_user_abort(true);
if (connection_aborted()) {
unlink($f);
}

this one will delete the file even if user cancel the download.

+2  A: 
unlink($filename);

This will delete the file.

inkedmn
ok but will it work while user downloading it ?
Ahmet vardar
Your best bet would be to not serve the file up directly from the disk and, instead, use a unique URL that you can disable after the download request.
inkedmn
my scenario is like that, when user hits download button, my script will create a temp. zip file and user download it then that temp zip file will be deleted
Ahmet vardar
@Ahmet vardar: Why not just echo out the content of the Zip?
Billy ONeal
@Billy: cuz if i do that, there will no download navigation for user
Ahmet vardar
@Ahmet vardar: What do you mean by that? You're calling `readfile`. `readfile` reads and echos a file. It makes more sense to just echo the data directly instead of saving it to a file and then calling `readfile` on it.
Billy ONeal
What about the accelerators that make multiple requests?
George Edison
@George Edison: *Bill turns off resume support. Accelerators unhappy. Bill happy :D
Billy ONeal
+5  A: 

There is no any correct way to detect whether file was completely downloaded by user or not.
So the best way will be to delete file after some period of inactivity.

zerkms
A: 

The safest way I can think of would be to use some client-side flash movie that can observe the transfer from the client side, and then make an XmlHttpRequest call to the server once the download finishes.

AFAIK, there's no way to do that reliably without using Flash (or a similar browser plugin)

timdev
A: 

It should work fine to just delete it after readfile because the content being transfered from the file will be read into memory and sent to the client by the webserver after reading it. From that point on, (after the readfile call) you can delete it.

Edit: On second thought, some clients will be using accelerators which will make multiple requests. Therefore, ignore what I've written.

George Edison
really? thats amazing
Ahmet vardar
oh no, right :(
Ahmet vardar
@George: the code based on `readfile` is not supporting Range. so requests after first will be just cancelled at all. this is work solution until he will be able to implement code that supports Range header.
zerkms
That's true too.
George Edison