Connecting Tech Pros Worldwide Forums | Help | Site Map

Finished file download

Bernhard
Guest
 
Posts: n/a
#1: Jul 17 '05
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
download?
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.)

Alvaro G. Vicario
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Finished file download


*** Bernhard escribió/wrote (Mon, 21 Mar 2005 14:05:47 +0000):[color=blue]
> 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.)[/color]

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');

readfile('foo.pdf');

update_my_cute_download_counter();

?>


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


--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (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
--
Bernhard
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Finished file download


Bernhard wrote:[color=blue]
> 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
> download?
> 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.)[/color]

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.

<?php
$file="music.mp3";
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header("Content-type: application/octet-stream\nContent-Disposition:
inline; filename=\"$file\"\nContent-length:
".(string)(filesize("/websites/test/$file")));

/* 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')) {
exit;
}

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

fclose($handle);

}
}
?>
Chung Leong
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Finished file download



"Alvaro G. Vicario" <kAlvaroNOSPAMTHANKS@terra.es> wrote in message
news:b14twbkybxx4$.14qkhey0ktxtj.dlg@40tude.net...[color=blue]
> *** Bernhard escribió/wrote (Mon, 21 Mar 2005 14:05:47 +0000):[color=green]
> > 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.)[/color]
>
> 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');
>
> readfile('foo.pdf');
>
> update_my_cute_download_counter();
>
> ?>
>
>
> Of course, it's really hard to say whether download was interrupted or[/color]
not.

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


R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Finished file download


Chung Leong wrote:
<snip>[color=blue][color=green]
> > Of course, it's really hard to say whether download was interrupted[/color][/color]
or[color=blue]
> not.
>
> To know for sure, just take a peek at the Apache access log.[/color]

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

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

Closed Thread