Forum Moderators: coopster

Message Too Old, No Replies

handle cancel download

         

Scally_Ally

9:52 am on May 24, 2007 (gmt 0)

10+ Year Member



Hi All,
I have a script that dynamically creates a bunch of hires jpegs and presents them to the user to download in a nice, easy, managable and lovely zip file (using exec command line function). When the user has downloaded the file all 'temp' images are then cleaned up and the zip file deleted so not to take up any space.

Problem is that if the user cancels the download the cleanup operation at the end of my script doesnt run - this will lead to problems later on as the next time that i create a zip that has the same filename as one that hasnt been cleaned up, instead of creating a new zip files will just be added to the one that is already on the server and not been deleted.

How do i handle the cancellation of a forced file download so that i can cleanup images / zips if they do this?

Here is a snip of my code.

for(all of my created jpegs){
$myFiles[] = "design".$des_num."_template".$t_ID.".jpg";
}
$execStr = "zip myZipFile.zip " . implode(" ", $myFiles);
exec($execStr);

$handle = fopen("myZipFile.zip", "r");
$contents = fread($handle, filesize("myZipFile.zip"));
fclose($handle);

//present the zip
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=myZipFile.zip;" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("myZipFile.zip"));
//echo out data
echo $contents;

foreach($myFiles as $key => $value){
unlink("/root/to/file/".$value);
}
//delete the zip
unlink("myZipFile.zip");

Cheers

Ally

[edited by: Scally_Ally at 9:53 am (utc) on May 24, 2007]

phparion

11:46 am on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



very simple,

put this

unlink("myZipFile.zip");

where your cancel download code resides.

Scally_Ally

1:25 pm on May 24, 2007 (gmt 0)

10+ Year Member



But where would that be?
as it is a forced download i cant say

if(!echo $contents){ unlink();}

or something similar..

Cheers
Ally

jatar_k

2:04 pm on May 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



since it's forced you can't do much to actually handle the case where they cancel it

you could have a cron cleanup script that would run to clean up any files left that were cancelled.

Scally_Ally

2:38 pm on May 24, 2007 (gmt 0)

10+ Year Member



Thought as much.. i suppose you run into probs if the browser crashes or the file isnt downloaded etc etc..

Was hopin there was gonna be a better way than that..

Thanks both for your help

Ally

jatar_k

2:41 pm on May 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



something I have done is name files to specific users. This works for repeat downliads. Then, even if they aren't cleaned up, the other file just gets overwritten and it doesn't cause large numbers of files to be produced.

It would only work in a specific scenario but it is something to think about.

phparion

5:20 am on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



with a little javascript you can track the window.event and if the user presses cancel button call the php script using simple ajax or even javascript and send the name to the php script which would clean the file from the server here is the dirty psuedocode

javascript

if(window.event == cancel button)

redirect= remove.php?filename=var file name

PHP

Get var file name,

unlink from the server

Also if you could post your complete code then we might give you a quick fix for it because i am assuming if user accepts to download you call a script in headers or whatever to pick the file from the server so must be using IF somewhere in the ELSE of that IF you can use remove.php code :)

joelgreen

5:39 pm on May 25, 2007 (gmt 0)

10+ Year Member



Look at this thread. You can determine canceled download from php code
[webmasterworld.com...]