Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when a user has successfully finished downloading a file in php

I've got a php page which handles requets for file downloads. I need to be able to detect when a file has been downloaded successfully. How can this be done? Perhaps there's some means of detecting this client-side then sending a confirmation down to the server.

Thanks.

Edit: By handle, I mean that the page is doing something like this:

$file = '/var/www/html/file-to-download.xyz';
  header('Content-Type: application/octet-stream');
  header('Content-Length: ' . filesize($file));
  header('Content-Disposition: attachment; filename=' . basename($file));
  readfile($file);
like image 451
sledgebox Avatar asked Oct 11 '08 20:10

sledgebox


People also ask

Why is my php file downloading instead of running?

This is normally due to an improper handler code. In the . htaccess file, you will want to ensure the handler code matches your version of php. If it does not, the php files may try to download instead of process.


1 Answers

Handle the download in a seperate php script (better do a little more than just readfile($file);, you can also provide the ability to resume downloads like in this question). Then in this script, when you read the last block and send it, you know that all the file was sent. This is not the same as knowing that all was received, but it should be enough for most scenarios.

like image 134
Treb Avatar answered Nov 12 '22 03:11

Treb