Finished file download

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am not sure if php can achieve this, but i guess that my problem
shoulb be solved with an server side language.

Is there any way i can tell if a visitor of my website has finished a
For example the visitor clicks on a file download link, and when his
download is finished then i want to happen something (for example some
download counter or something like that.)

Re: Finished file download

*** Bernhard escribió/wrote (Mon, 21 Mar 2005 14:05:47 +0000):
Quoted text here. Click to load it

I'm not really sure but this approach _may_ work:


header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=foo.pdf;');
header('Content-Length: ' . filesize('foo.pdf');




Of course, it's really hard to say whether download was interrupted or not.

-+ Álvaro G. Vicario - Burgos, Spain
+- (la web de humor barnizada para la intemperie)
++ No envíes tu dudas a mi correo, publícalas en el grupo
-+ Do not send me your questions, post them to the group

Re: Finished file download

Quoted text here. Click to load it

To know for sure, just take a peek at the Apache access log.

Re: Finished file download

Chung Leong wrote:
Quoted text here. Click to load it

   At least for me, the access log has no information about the

  <?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: /

Re: Finished file download

Bernhard wrote:
Quoted text here. Click to load it

I have also found the this solution. It seems to be quiet robust.
You can also tell if a file download is not finished, because then
$bytesSent is too small.

header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header("Content-type: application/octet-stream\nContent-Disposition:
inline; filename=\"$file\"\nContent-length:

/* fpassthru is apparantly a memory-hog. Use this instead */
    $fp = fopen($file, 'r');
    while(!feof($fp)) {
        $buf = fread($fp, 4096);
        echo $buf;
        $bytesSent+=strlen($buf);    /* We know how many bytes were sent
to the user */

    if($bytesSent==filesize($file)) {
        $filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {



Site Timeline