473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Finished file download

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.)
Jul 17 '05 #1
4 2957
*** Bernhard escribió/wrote (Mon, 21 Mar 2005 14:05:47 +0000):
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'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
--
Jul 17 '05 #2
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);

}
}
?>
Jul 17 '05 #3

"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message
news:b1*****************************@40tude.net...
*** Bernhard escribió/wrote (Mon, 21 Mar 2005 14:05:47 +0000):
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'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.

To know for sure, just take a peek at the Apache access log.
Jul 17 '05 #4
Chung Leong wrote:
<snip>
Of course, it's really hard to say whether download was interrupted
or not.

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


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/

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Peter A. Schott | last post by:
If I want to verify that a file has finished writing before deleting the remote file, what would be the best method? Current code on Python 2.4: #filename - remote FTP server File Name...
4
by: Doug R | last post by:
Hello, I could use a little help from you Gurus out there. I have an aplication that watches a directory and detects when a PGP encrypted file lands in the directory and starts a process that...
0
by: Buddy Ackerman | last post by:
I am trying to implment a file download via a link such that when clicked, instead of starting the default application for that type of file the user will be presented with a download dialog...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
4
by: Andreas.Burman | last post by:
Hi I call a program through exec that can take a long time to execute. This is fine but I would like to show some kind of animation or something while the program is running and when it is...
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
4
by: Roberto Mora | last post by:
I have not done programming in a very long time and what is worst, I never learned VB. Although my job does not require this knowledge, I cam across a problem that although it seemed simple it has...
13
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.