Bernhard wrote:
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.)
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);
}
}
?>