473,563 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check if file was downloaded

Hi,

I use the following code to offer a download:

header("Content-Type:$mimetype" );
header("Content-Disposition: attachment; filename=$filen ame");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Length:".filesi ze($temp_filena me));
readfile_chunke d($temp_filenam e);

(readfile_chunk ed 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 10314
Rik
On Fri, 23 Feb 2007 15:17:20 +0100, Hendri Adriaens
<sp**********@T HISgmail.comwro te:
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_abo rt(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_aborte d()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abo rt() to
true.
}

--
Rik Wasmus
Feb 23 '07 #2
I'd use a combo of:
>
ignore_user_abo rt(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_aborte d()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abo rt() 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**********@T HISgmail.comwro te:
>I'd use a combo of:

ignore_user_ab ort(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_abort ed()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abo rt() 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_chunke d is in order here, or you
server buffers output?
--
Rik Wasmus
Feb 23 '07 #4
Hmnmz, perhaps a flush() after readfile_chunke d 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_chunke d script. But
here it is for your reference:

function readfile_chunke d($filename,$re tbytes=true)
{ # from: http://nl2.php.net/manual/nl/function.readfile.php
$chunksize=1*(1 024*1024); // how many bytes per chunk
$buffer='';
$cnt=0;
$handle=fopen($ filename, 'rb');
if ($handle===fals e) {
return false;
}
while (!feof($handle) )
{
$buffer=fread($ handle,$chunksi ze);
echo $buffer;
ob_flush();
flush();
if ($retbytes)
{
$cnt+=strlen($b uffer);
}
}
$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**********@T HISgmail.comwro te:
>I'd use a combo of:

ignore_user_ab ort(true);
readfile();
//just for those fast downloads of small files sleep
//tinker with it
sleep(2);
if(!user_abort ed()){
unlink();
} else {
//perhaps some logging, which is why I set the ignore_user_abo rt() 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_abor ted()....
--
Rik Wasmus
Feb 23 '07 #6
Silly me.... replace user_aborted() with connection_abor ted()....

Sorry, but now the code following readfile_chunke d 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**********@T HISgmail.comwro te:
>Silly me.... replace user_aborted() with connection_abor ted()....

Sorry, but now the code following readfile_chunke d 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**********@T HISgmail.comwro te:
>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

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

Similar topics

2
5469
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 photo size to 1 meg. I would like to check the file size in JavaScript on the page, instead of on the server. Anyone know how I can check the file...
2
5342
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 Database Public Function adhFileExists(strName As String) As Boolean On Error Resume Next
7
33458
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
2592
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 file size (client or server side), so that error doesn't occur? Thanks --Dietrich
3
3673
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 by another process. This occurs even if I release the object using fs.Close() and fs = Nothing. Please help (my code is given below) Best...
3
30192
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 trying to upload file size more than 4MB then just display a message that file size is too big and can't be uploaded. thanks
1
4265
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 trying to write to the file and is throwing an exception (File... in use by another process) whenever the output file (strFileName) is open. I am...
2
1164
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 downloading i get the message windows cannot open file. Then i have two choices, use web service to find appropiate program or select program from a list. ...
2
9802
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 the document headers, not the data. request.Credentials = System.Net.CredentialCache.DefaultCredentials; // This may throw a WebException:
3
8252
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 the file size; however, I'm not happy with this solution because my application is designed to work in multiple browsers and ActiveX is limited to...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.