I am creating zip files dynamically which user should download and after download is complete I want to remove the file .I think all JS ways are not safe . So not a secret that php it is a server side language and this makes me think that in some way there must be away that between that client and my server bytes are no longer transmitted so I can tell that now the download is complete . IS there a similar way of accomplishing what I described above ?
-
Possible duplicate http://stackoverflow.com/questions/4905664/detect-if-download-is-complete , http://stackoverflow.com/questions/1563187/check-if-download-is-completed – Adi Aug 14 '12 at 11:57
-
Already been answered: http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – thedethfox Aug 14 '12 at 11:58
-
http://stackoverflow.com/questions/1563187/check-if-download-is-completed – Kerem Aug 14 '12 at 12:03
3 Answers
as far as i know there is no way to know when a user has finished the download.
I would store the files in a separate queue and delete them after a certain time has passed (something between 1h and 1d)

- 1,320
- 10
- 20
-
what if a bad guy does the same thing dozen of times and the server fulls servers memory ? – user1485518 Aug 15 '12 at 09:14
-
Well you have the same problem even if you delete the file the moment the user has finished downloading. i.E. the bad guy simulates a slow connection or disables the response that tells you about the finished download. If you are afraid of some people abusing the system you should think of some other way to distribute the download (perhaps send the link per email only to registered users and then only a few files per hour) – wodka Aug 15 '12 at 14:45
why dont you let php to handle the download?
//you may need setting php runtime limit to zero.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
$filename = "myfile.zip";
$file = file_get_contents($filename);
echo $file;
//download complete
unlink($filename); //delete file
NOTICE: in my test, i wasn't deleting, instead i was giving output to syslog as "download complete" then watch for syslog and run the script,
it was saying download complete as download seems ~1900KB/2000KB it's probably a chrome problem but you can use sleep(3); and wait for 3 seconds before deleting file to have sure not to have problems.
EDIT: This script deletes the file even if the download fails for any reason. you may need to create file for each download attempt. I would go with:
1- create file 2- user downloads/fails 3- remove file 4- if user wants to download again: go to 1

- 509
- 4
- 9
If you're handling the download in PHP, you can use ignore_user_abort() to keep your script running in the event the connection breaks and check connection_aborted() as you send the data in chunks. If an abort occurs before all chunks have been set, then the download failed. Since the web server buffers the PHP output, there's no guarantee that the transfer has really reached the end.
Another possible way to do this is set up a piped Apache log that would send event notifications to a PHP script. That will work even when the download isn't handle by PHP itself. I believe Apache wouldn't flag the request as having completed with HTTP 200 unless the server receives a TCP/IP acknowledgement that the last packets have arrived safely.

- 7,242
- 4
- 31
- 40