473,396 Members | 1,966 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.

check if file was downloaded

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
11 10283
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tgundeck | last post by:
Does anyone know how I can check the file size of photos or any file type in JavaScript? I'm allowing users to store photos in my web page using the input type = file tag, but need to limit the...
2
by: David | last post by:
I'm using following code for checking a file existence. I's working fine for given folder. Is there a way to check a file exitance in subfolders? Thanks in advance, David Option Compare...
7
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
4
by: lawrence | last post by:
Using <INPUT type"file" runat="server"> to upload a file. When the file size is too large I get a "page cannot be display" error. Thats cool I'm down with that. But is there a way to check for...
3
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use...
3
by: puja | last post by:
hi all, In asp.net 2.0 there is a limit on file size upload of 4MB. I know this limit can be increased in web.config file. Is there any way to check the file size before upload ? if user is...
1
by: jtertin | last post by:
I am currently using the following code to make sure a file is writable (i.e. is not in use) by using the CanWrite method of the FileStream object. Using the code below, the TextWriter is still...
2
by: shadow2284 | last post by:
i'm trying to download full tilt poker and i've downloaded it before but i reformatted my computer and now when i go to download it it says i'm downloading "download.php" when it's finished...
2
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get...
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.