PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
i am looking for a method to update a counter everytime a download has been completed. at the moment the counter updates if the download has a/ completed b/ timedout or c/ cancelled (even if its hit & cancelled)
// Now send the file with header() magic
header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: Application/octet-stream");
header("Content-disposition: attachment; filename=" . $downloads->fields['orders_products_filename']);
$zv_filesize = filesize(DIR_FS_DOWNLOAD . $downloads->fields['orders_products_filename']);
//@ignore_user_abort(true); // when not commented out the counter never updates
if (DOWNLOAD_BY_REDIRECT == 'true') {
// This will work only on Unix/Linux hosts
zen_unlink_temp_dir(DIR_FS_DOWNLOAD_PUBLIC);
$tempdir = zen_random_name();
umask(0000);
mkdir(DIR_FS_DOWNLOAD_PUBLIC . $tempdir, 0777);
symlink(DIR_FS_DOWNLOAD . $downloads->fields['orders_products_filename'], DIR_FS_DOWNLOAD_PUBLIC . $tempdir . '/' . $downloads->fields['orders_products_filename']);
if ((connection_status()==0) and !connection_aborted() ) {
//update counter
$db->Execute("update " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . " set download_count = download_count-1 where orders_products_download_id = '" . (int)$_GET['id'] . "'");
} else {
//download aborted
}
zen_redirect(DIR_WS_DOWNLOAD_PUBLIC . $tempdir . '/' . $downloads->fields['orders_products_filename']);
} else {
// This will work on all systems, but will need considerable resources
// We could also loop with fread($fp, 4096) to save memory
header("Content-Length: " . $zv_filesize);
readfile(DIR_FS_DOWNLOAD . $downloads->fields['orders_products_filename']);
}
As far as I know, there is no way to detect whether or not a download has completed (at least not with PHP)...
FYI, I suggest you read the manual on connection_status() and connection_aborted().
They are used for detecting if the page execution has been aborted.. not downloads.
If my idea would not work simply because the code would never run, then send it just before the last byte gets flushed to the browser... (by sending a certain amount of byes at a time inside a loop, when the current byte + the increment > total filesize you know you are on the last iteration of the loop and its time to update the counter)