473,386 Members | 2,114 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,386 software developers and data experts.

How to detect if file download completed or cancelled

I have a large install file (an exe) on my web server that people
download and install from. Looking at my log files, I see a lot of
people downloading it, but no way to tell for sure if they completed the
download or cancelled out before it completed. Is there any function in
PHP that would allow the web server to send the file and detect a
completion or cancellation? Or perpahs a javascript/PHP method?

Any help would be greatly appreciated.
Thanks,
Dan
Oct 18 '06 #1
2 23684
You could check the byte count in the web server log to see if the
whole file was transferred.

In PHP, you could also do this by checking connection_aborted() after
sending the file. For example:

ignore_user_abort(true); // Don't end if the connection breaks

if (readfile($path) !== false && !connection_aborted()) {
// Success!
}

Here's a larger example. The function sendFile returns the status of
the download:

<?

// Sample usage

function sendTest()
{
$res = sendFile('application.exe', 'application/octet-stream');

if ($res['status']) {
// Download succeeded
} else {
// Download failed
}

@saveDownloadStatus($res);
}

// The sendFile function streams the file and checks if the
// connection was aborted.

function sendFile($path, $contentType = 'application/octet-stream')
{
ignore_user_abort(true);

header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' .
basename($path) . "\";");
header("Content-Type: $contentType");

$res = array(
'status' =false,
'errors' =array(),
'readfileStatus' =null,
'aborted' =false
);

$res['readfileStatus'] = readfile($path);
if ($res['readfileStatus'] === false) {
$res['errors'][] = 'readfile failed.';
$res['status'] = false;
}

if (connection_aborted()) {
$res['errors'][] = 'Connection aborted.';
$res['aborted'] = true;
$res['status'] = false;
}

return $res;
}

// Save the status of the download to some place

function saveDownloadStatus($res)
{
$ok = false;
$fh = fopen('download-status-' . $_SERVER['REMOTE_ADDR'] . '-' .
date('Ymd_His'), 'w');
if ($fh) {
$ok = true;
if (!fwrite($fh, var_export($res, true))) {
$ok = false;
}
if (!fclose($fh)) {
$ok = false;
}
}
return $ok;
}

Dan D wrote:
I have a large install file (an exe) on my web server that people
download and install from. Looking at my log files, I see a lot of
people downloading it, but no way to tell for sure if they completed the
download or cancelled out before it completed. Is there any function in
PHP that would allow the web server to send the file and detect a
completion or cancellation? Or perpahs a javascript/PHP method?

Any help would be greatly appreciated.
Thanks,
Dan
Oct 20 '06 #2
Thank you very much. That looks like it should work great. I'll give it
a try.
Dan

pe*******@gmail.com wrote:
You could check the byte count in the web server log to see if the
whole file was transferred.

In PHP, you could also do this by checking connection_aborted() after
sending the file. For example:

ignore_user_abort(true); // Don't end if the connection breaks

if (readfile($path) !== false && !connection_aborted()) {
// Success!
}

Here's a larger example. The function sendFile returns the status of
the download:

<?

// Sample usage

function sendTest()
{
$res = sendFile('application.exe', 'application/octet-stream');

if ($res['status']) {
// Download succeeded
} else {
// Download failed
}

@saveDownloadStatus($res);
}

// The sendFile function streams the file and checks if the
// connection was aborted.

function sendFile($path, $contentType = 'application/octet-stream')
{
ignore_user_abort(true);

header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' .
basename($path) . "\";");
header("Content-Type: $contentType");

$res = array(
'status' =false,
'errors' =array(),
'readfileStatus' =null,
'aborted' =false
);

$res['readfileStatus'] = readfile($path);
if ($res['readfileStatus'] === false) {
$res['errors'][] = 'readfile failed.';
$res['status'] = false;
}

if (connection_aborted()) {
$res['errors'][] = 'Connection aborted.';
$res['aborted'] = true;
$res['status'] = false;
}

return $res;
}

// Save the status of the download to some place

function saveDownloadStatus($res)
{
$ok = false;
$fh = fopen('download-status-' . $_SERVER['REMOTE_ADDR'] . '-' .
date('Ymd_His'), 'w');
if ($fh) {
$ok = true;
if (!fwrite($fh, var_export($res, true))) {
$ok = false;
}
if (!fclose($fh)) {
$ok = false;
}
}
return $ok;
}

Dan D wrote:
>>I have a large install file (an exe) on my web server that people
download and install from. Looking at my log files, I see a lot of
people downloading it, but no way to tell for sure if they completed the
download or cancelled out before it completed. Is there any function in
PHP that would allow the web server to send the file and detect a
completion or cancellation? Or perpahs a javascript/PHP method?

Any help would be greatly appreciated.
Thanks,
Dan

Oct 23 '06 #3

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

Similar topics

8
by: IGC | last post by:
I have a requirement to record in a database when a file is finished downloading to the end-user. Currently when a user clicks a download icon for a file it directs to an ASP page that records the...
1
by: alocin | last post by:
in my aspx page's page_load i got: Dim imageData() As Byte=allegato Response.ClearContent() Response.ClearHeaders() Response.AppendHeader("Content-Length", imageData.Length.ToString)...
5
by: RC | last post by:
how to detect file is completely downloaded from client by following code? <%@ Page language="C#" debug="true" %> <script language="C#" runat="server"> private void Page_Load(object sender,...
5
by: Dan D | last post by:
I have a large install file (an exe) on my web Apache server that people download and install from. Looking at my log files, I see a lot of people downloading it, but no way to tell for sure if...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
1
by: test | last post by:
I'm trying to develop a small inhouse file download tool I need to detect all files downloaded from internetexplorer and display a custom interface i.e. when ever the user tries to download a file...
2
by: Mark Anderson | last post by:
Is it possible to identify the action of a user cancelling by the user of file download dialog? (as opposed to something else being captured. Ideally I'd like to do this for a IE6+/FF v1.5+/Safari...
1
by: Tim B | last post by:
Is there a way in javascript to detect that the file download dialog box has appeared or has closed? Suppose I have a form that when submitted makes a request that could take awhile to complete....
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.