473,327 Members | 1,892 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,327 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 23644
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.