By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

check if file was downloaded

P: n/a
Hi,

I use the following code to offer a download:

header("Content-Type:$mimetype");
header("Content-Disposition: attachment; filename=$filename");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Length:".filesize($temp_filename));
readfile_chunked($temp_filename);

(readfile_chunked comes from
http://nl2.php.net/manual/nl/function.readfile.php)

My question: how can I check if the file was downloaded and saved? I want to
make an automatic removal possible after download.

The function seems to exit when the user clicks cancel and doesn't download
the file, so all following commands will not get executed. That's perfect.

However, it doesn't when the file is small enough to fit in a chunk. php's
readfile seems to have the same behaviour. So, in that case, all following
commands do get executed, even if cancel was clicked (and in my application,
the file then gets removed, which shouldn't happen of course).

Also checking the bytes sent doesn't help. If the function exists, it always
returns the complete filesize for the small files that fit in a single
chunk, even if cancel was clicked.

So, is there any working way to determine if a file was downloaded and
opened/saved instead of cancel being clicked in the download dialog?

Thanks in advance, best regards,
-Hendri Adriaens.
Feb 23 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Rik
On Fri, 23 Feb 2007 15:17:20 +0100, Hendri Adriaens
<sp**********@THISgmail.comwrote:
So, is there any working way to determine if a file was downloaded and
opened/saved instead of cancel being clicked in the download dialog?
I'd use a combo of:

ignore_user_abort(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_aborted()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abort() to
true.
}

--
Rik Wasmus
Feb 23 '07 #2

P: n/a
I'd use a combo of:
>
ignore_user_abort(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_aborted()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abort() to
true.
}
Thanks, but unfortunately, this doesn't work. Now, the remaining part of the
code is never executed. Also not when the file is saved or opened. And I
should note that the sleep only delays the save dialog to appear, so it
seems to be executed before readfile(), while I did put them in the right
order.

Do you maybe have another idea?

Thanks, best regards,
-Hendri.
Feb 23 '07 #3

P: n/a
Rik
On Fri, 23 Feb 2007 16:31:57 +0100, Hendri Adriaens
<sp**********@THISgmail.comwrote:
>I'd use a combo of:

ignore_user_abort(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_aborted()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abort() to
true.
}

Thanks, but unfortunately, this doesn't work. Now, the remaining part of
the
code is never executed. Also not when the file is saved or opened. And I
should note that the sleep only delays the save dialog to appear, so it
seems to be executed before readfile(), while I did put them in the right
order.

Do you maybe have another idea?
Hmnmz, perhaps a flush() after readfile_chunked is in order here, or you
server buffers output?
--
Rik Wasmus
Feb 23 '07 #4

P: n/a
Hmnmz, perhaps a flush() after readfile_chunked is in order here,

Sorry, it still doesn't work.
or you server buffers output?
I don't know that exactly, I didn't write the readfile_chunked script. But
here it is for your reference:

function readfile_chunked($filename,$retbytes=true)
{ # from: http://nl2.php.net/manual/nl/function.readfile.php
$chunksize=1*(1024*1024); // how many bytes per chunk
$buffer='';
$cnt=0;
$handle=fopen($filename, 'rb');
if ($handle===false) {
return false;
}
while (!feof($handle))
{
$buffer=fread($handle,$chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes)
{
$cnt+=strlen($buffer);
}
}
$status=fclose($handle);
if ($retbytes && $status)
{
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}

Best regards,
-Hendri.
Feb 23 '07 #5

P: n/a
Rik
On Fri, 23 Feb 2007 16:31:57 +0100, Hendri Adriaens
<sp**********@THISgmail.comwrote:
>I'd use a combo of:

ignore_user_abort(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_aborted()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abort() to
true.
}

Thanks, but unfortunately, this doesn't work. Now, the remaining part of
the
code is never executed. Also not when the file is saved or opened. And I
should note that the sleep only delays the save dialog to appear, so it
seems to be executed before readfile(), while I did put them in the right
order.
Silly me.... replace user_aborted() with connection_aborted()....
--
Rik Wasmus
Feb 23 '07 #6

P: n/a
Silly me.... replace user_aborted() with connection_aborted()....

Sorry, but now the code following readfile_chunked always gets executed for
small files, even if cancel was clicked. So it's the same as the original
situation, unfortunately.

-Hendri.
Feb 23 '07 #7

P: n/a
Rik
On Fri, 23 Feb 2007 16:56:07 +0100, Hendri Adriaens
<sp**********@THISgmail.comwrote:
>Silly me.... replace user_aborted() with connection_aborted()....

Sorry, but now the code following readfile_chunked always gets executed
for
small files, even if cancel was clicked. So it's the same as the original
situation, unfortunately.
Hmmmz, another solution could be, if these problems only occur with small
files, to make the chunk size drastically maybe that helps...

Then again:
If the user hits cancel, but the file is already downloaded (most browsers
start immediately), there's no way for php to distinguish that from an
actual download. The connection is already over & done. So if the file is
received earlier then the user has hit the cancel button, nothing can be
done.

--
Rik Wasmus
Feb 23 '07 #8

P: n/a
Hmmmz, another solution could be, if these problems only occur with small
files, to make the chunk size drastically maybe that helps...
You mean make the chucksize very small? I guess that leads to very slow
downloads?
Then again:
If the user hits cancel, but the file is already downloaded (most browsers
start immediately), there's no way for php to distinguish that from an
actual download. The connection is already over & done. So if the file is
received earlier then the user has hit the cancel button, nothing can be
done.
Is there no way to physically start the download only if the user hit
something else than cancel?

Best regards,
-Hendri.
Feb 23 '07 #9

P: n/a
Rik
On Fri, 23 Feb 2007 17:10:26 +0100, Hendri Adriaens
<sp**********@THISgmail.comwrote:
>Hmmmz, another solution could be, if these problems only occur with
small
files, to make the chunk size drastically maybe that helps...

You mean make the chucksize very small? I guess that leads to very slow
downloads?
Well, not really, somewhat slower, but mainly somewhat headvier on the
server.
>Then again:
If the user hits cancel, but the file is already downloaded (most
browsers
start immediately), there's no way for php to distinguish that from an
actual download. The connection is already over & done. So if the file
is
received earlier then the user has hit the cancel button, nothing can be
done.

Is there no way to physically start the download only if the user hit
something else than cancel?

Nope, it's in the browser itself, the cancelling of a download isn't a
HTTP feature. You can add a dialogue on the page, or possibly something
with javascript, it will not help: the browser dialogue will still come
into play in the end.

There may be a possibility to load the file into a hidden frame or
something, and to check that with javascript. This will not help your php
script, but javascript could fire a request on succes, ans you only delete
the file on that second request. It would mean nothing get's deleted with
javascript disabled, and I'm a little bit fuzzy on the details...

Then again: I do not know what the exact purpose of you script is in
general, maybe it's OK to keep the small files and delete them
periodically after a check with filemtime()?
--
Rik Wasmus
Feb 23 '07 #10

P: n/a
Well, not really, somewhat slower, but mainly somewhat headvier on the
server.
Ok, thanks. So, a chunksize of 1 byte should be optimal for my purpose. I
will
test the load differences for the server.
Nope, it's in the browser itself, the cancelling of a download isn't a
HTTP feature. You can add a dialogue on the page, or possibly something
with javascript, it will not help: the browser dialogue will still come
into play in the end.
Bad luck.
There may be a possibility to load the file into a hidden frame or
something, and to check that with javascript. This will not help your php
script, but javascript could fire a request on succes, ans you only delete
the file on that second request. It would mean nothing get's deleted with
javascript disabled, and I'm a little bit fuzzy on the details...
Ok, no problem, me neither.
Then again: I do not know what the exact purpose of you script is in
general, maybe it's OK to keep the small files and delete them
periodically after a check with filemtime()?
I just wanted to offer users of a sharing environment to remove the file
from
the server automatically after the file was downloaded. So a period clean up
is not really what I want. I don't want to remove files that shouldn't be
removed.

But thanks for your help and answers! But if you find any other ideas than
chunksize=1, I will be more than happy to test them :)

Best regards,
-Hendri.
Feb 23 '07 #11

P: n/a
Ok, thanks. So, a chunksize of 1 byte should be optimal for my purpose. I
will
test the load differences for the server.
Strangely enough, this doesn't work either...

I have 3 files:
a 19.4 MB
b 159 KB
c 1.03KB

With the previous chunksize (1*1024*1024 bytes), the script continued even
when cancel was clicked for files b and c, but not for a. With a chunksize
of 1 byte, it doens't continue for a and b, but still does for c. So the
script would still conclude that it was downloaded even if cancel was
clicked.

If anyone has an idea how this works and how I could get a script to
correctly report whether a file was actually downloaded or not, please let
me know.

Thanks, best regards,
-Hendri.
Feb 23 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.