complete downloads

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!

Moderator: General Moderators

Post Reply
xylar
Forum Newbie
Posts: 7
Joined: Sun Feb 19, 2006 2:07 pm

complete downloads

Post by xylar »

hi

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)

at the moment the code is the following:

Code: Select all

// 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']);
  }
any help would be most appreciated
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

move the code that updates the count to after the file has been sent
xylar
Forum Newbie
Posts: 7
Joined: Sun Feb 19, 2006 2:07 pm

Post by xylar »

if i do that the counter doesn't update at all, even after completed downloads
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

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.
xylar
Forum Newbie
Posts: 7
Joined: Sun Feb 19, 2006 2:07 pm

Post by xylar »

i was given a hint by someone (who has got it working with downloads) to look at the function 'connection_aborted'

maybe i should ask him for another hint :)
xylar
Forum Newbie
Posts: 7
Joined: Sun Feb 19, 2006 2:07 pm

Post by xylar »

still not found a solution, so any thoughts/input would be great :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

We'll, there is a PHP progress bar, so I'm sure if you took a look over at http://sourceforge.net/projects/megaupload/ you should be able to tweak it for your needs..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

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)
xylar
Forum Newbie
Posts: 7
Joined: Sun Feb 19, 2006 2:07 pm

Post by xylar »

thanks for the link Jcart, i took a look at the code but its only aimed at uploads and i couldnt find any helpful code.

thanks jshpro2 for the suggestion i am giving it a go atm, i also found the follwoing link, was thinking i might try to incorporate it into my code:

http://svn.textpattern.com/development/ ... b_file.php

also found a topic on this forum have yet it to try it, there appears to be some strange code in there, eg/ '}'

viewtopic.php?t=16879&postdays=0&postor ... d&start=15
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

also found a topic on this forum have yet it to try it, there appears to be some strange code in there, eg/ '}'

viewtopic.php?t=16879&postdays=0&postor ... d&start=15
Unfortunantly we had some issues with the database where some code was converted to their html entities.. :(

try running htmlentities_decode() to recover it
Post Reply