473,493 Members | 4,355 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A follow-up to my download-all-files-as-a-zip-file post

I made a script that successfully creates a .zip file of all the files
in a directory on my web server, but now what I haven't figured out
how to do is how to have it automatically deleted when the user
successfully downloads it, as otherwise my server would eventually get
clogged up with all these zip files.

Any help/suggestions?

Thanks

Aug 5 '07 #1
5 2210
Rik
On Sun, 05 Aug 2007 03:29:38 +0200, <te******@gmail.comwrote:
I made a script that successfully creates a .zip file of all the files
in a directory on my web server, but now what I haven't figured out
how to do is how to have it automatically deleted when the user
successfully downloads it, as otherwise my server would eventually get
clogged up with all these zip files.

Any help/suggestions?
If you're serving it using PHP itself, a construct _might_ be possible by
using ignore_user_abort() and checking the connection_status() at the end
(after all data is sent), and unlink the file if there's been no abortion.
However, ths smaller the file, the less trustworthy it becomes (Think of
the UA: you ask to download, you abort, but if it's allready in, the
connection was succesfull. Now try to download it again, and it's
gone...). If all is reproducable (i.e. the original files/data remains,
only the zip files need to be cleaned up) the only problem is wasted
resources. If you're deleting data you can't get again it's not wise to
delete anything unless you're really, really sure.

This has been discussed in this group thoroughly a few months ago, try to
search the archives. I think the ignore_user_abort/unlink functions are
mentioned in it, so it's a place to start searching.

From the top of my head, I think the final solution was it's better to ask
for a user confirmation, and temporary clean old files up using a cron job.
--
Rik Wasmus
Aug 5 '07 #2
te******@gmail.com wrote:
I made a script that successfully creates a .zip file of all the files
in a directory on my web server, but now what I haven't figured out
how to do is how to have it automatically deleted when the user
successfully downloads it, as otherwise my server would eventually get
clogged up with all these zip files.

Any help/suggestions?

Thanks
I'd probably just create a cron job that deletes any zip files older than
a day. That's the simplest way I can think of. Of course, depending on the
amount of disk space you have available, and how trustworthy your users are,
this could open you up to a DOS attack of sorts. A person could keep clicking
on the download link (or automate it) and possibly fill up your whole disk in
a day.

Aug 5 '07 #3
On Aug 4, 8:04 pm, Matt Madrid <admiral...@gmail.comwrote:
techu...@gmail.com wrote:
I made a script that successfully creates a .zip file of all the files
in a directory on my web server, but now what I haven't figured out
how to do is how to have it automatically deleted when the user
successfully downloads it, as otherwise my server would eventually get
clogged up with all these zip files.
Any help/suggestions?
Thanks

I'd probably just create a cron job that deletes any zip files older than
a day. That's the simplest way I can think of. Of course, depending on the
amount of disk space you have available, and how trustworthy your users are,
this could open you up to a DOS attack of sorts. A person could keep clicking
on the download link (or automate it) and possibly fill up your whole disk in
a day.
I had a thought during dinner, but I'm not sure if this would
*actually* work or not:

Could I initiate a session when a user navigates to a folder, and when
they leave that particular folder destroy the session which I could
then trigger using a simple if-statement to delete the .zip file?

Aug 5 '07 #4
Matt Madrid wrote:
I'd probably just create a cron job that deletes any zip files older than
a day. That's the simplest way I can think of. Of course, depending on the
amount of disk space you have available, and how trustworthy your users are,
this could open you up to a DOS attack of sorts. A person could keep clicking
on the download link (or automate it) and possibly fill up your whole disk in
a day.
A solution which would solve both the "when to delete" question, and the
possibility of DOS attacks would be to simply *keep* all the ZIP files as a
*cache*. This should actually *reduce* the load on the server.

Sketch of how it would work, assuming $X is the place where you keep these
ZIP files you've made...

1. User requests a ZIPped directory. Call this directory $D.

2. You scan through each file in $D to find the "last modified"
date of the *newest* file in $D.

3. Call the timestamp of that last modification $T.

4. Does there exist a file "$X/$D.zip"? If not, go to step 9.

5. Is the last-modified date of "$X/$D.zip" older than $T?
If so, go to step 8.

6. Serve up "$X/$D.zip".

7. Exit.

8. Delete "$X/$D.zip".

9. Zip up directory $D and call it "$X/$D.zip".

10. Use the UNIX "touch" command to change the last-modified
and created dates of "$X/$D.zip" to $T.

11. Go to step 6.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 45 days, 11:47.]

Command Line Interfaces, Again
http://tobyinkster.co.uk/blog/2007/0...nd-line-again/
Aug 5 '07 #5
te******@gmail.com wrote:
On Aug 4, 8:04 pm, Matt Madrid <admiral...@gmail.comwrote:
>techu...@gmail.com wrote:
>>I made a script that successfully creates a .zip file of all the files
in a directory on my web server, but now what I haven't figured out
how to do is how to have it automatically deleted when the user
successfully downloads it, as otherwise my server would eventually get
clogged up with all these zip files.
Any help/suggestions?
Thanks
I'd probably just create a cron job that deletes any zip files older than
a day. That's the simplest way I can think of. Of course, depending on the
amount of disk space you have available, and how trustworthy your users are,
this could open you up to a DOS attack of sorts. A person could keep clicking
on the download link (or automate it) and possibly fill up your whole disk in
a day.

I had a thought during dinner, but I'm not sure if this would
*actually* work or not:

Could I initiate a session when a user navigates to a folder, and when
they leave that particular folder destroy the session which I could
then trigger using a simple if-statement to delete the .zip file?
Well, that could work. I guess you mean something like this:

if (get_cwd() != $_SESSION['zip_directory']) {
delete_zip_file($_SESSION['zip_file']);
unset($_SESSION['zip_directory']);
unset($_SESSION['zip_file']);
}

Well, what happens if the user opens another browser tab while the download
is in progress. Then the cwd might be different, and the zip file would be
deleted in the middle of the transfer. Actually, there would probably be a
"file in use" error, so the file would never be deleted.

How are you doing the download? A direct link to the file, or streaming it
from the php script? I mentioned before that you could use header() and
file_get_contents to send the file. That might be your best bet here, except
that I would use passthru() instead.

You could then create a class and use its destructor method to delete the file.

Something like this (quick code.. but tested):

---------------

set_time_limit(0);
class zipit {
public $zipfile;
public $filename;
public function zip($directory) {
$tmp_file = "/tmp/zipfile".md5(microtime()).".zip";
$this->filename = basename($directory) . ".zip";
exec("zip -rj $tmp_file $directory",$output,$retval);
if (!$retval) {
$this->zipfile = $tmp_file;
return true;
}
else {
return false;
}
}
public function download() {
if (!is_readable($this->zipfile)) {
die("Something went wrong");
}
header("Content-type: application/zip");
header('Content-Disposition: file; filename="' . $this->filename);
passthru("cat ". $this->zipfile);
}
function __destruct() {
// delete the temporary zip file
unlink($this->zipfile);
}
}

$z = new zipit();

if ($z->zip("/tmp/nothing")) {
$z->download();
}

---------------

Hope this helps..

Matt M.

Aug 5 '07 #6

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

Similar topics

28
2318
by: Mark Carter | last post by:
I am using Windows 98, python 2.3, ClientCookie 0.4.3a. When I do: import ClientCookie import os c = ClientCookie.MSIECookieJar(delayload=1) c.load_from_registry() I get the response:...
2
1723
by: TurboDuster | last post by:
Ok, I couldn't get my page to POST to my server I thought because I had run IIS lockdown and other hardening tools. Now I've rebuilt the whole server from scratch and have yet to disable anything....
5
1887
by: Ken | last post by:
Hi. This is a followup to my NULL macro vs. 0 question. It's in a separate thread, because I need to post using google, so my original thread isn't available to me yet :(. Anyway, I just...
0
1291
by: MAFDoit | last post by:
NEWSGROUP: This is a followup to the post below. The original post was helpful and I now know many ways to RETRIEVE information from Windows explorer, but I haven't yet found a way to INSERT or...
1
12413
by: David Waz... | last post by:
My base path is c:\WINDOWS\Microsoft.Net..... etc NOT C:\WinNt... So, on my machine, the machine.config is under WINDOWS, not WinNt...
24
1885
by: Sta12s | last post by:
First of all, I have NO idea what I'm doing - I'm a complete newb to Javascript and PHP so my code is going to look like crap ;) Now for my problem, I'm trying to do a little quiz with...
10
6572
by: David Bear | last post by:
Being new to pgdb, I'm finding there are lot of things I don't understand when I read the PEP and the sparse documentation on pgdb. I was hoping there would be a module that would properly escape...
4
3460
by: Duffman | last post by:
Hello, I am working with dynamically created WSDL's and I need to change the transaction urls from http to https in the WSDL without redirecting customers to a different location. (I'm using vs...
8
3493
by: Laith Zraikat | last post by:
I am trying to invoke a post request from code behind of an asp.net page using "WebClient" object, and I want the user to be redirected to the action url as well. So far Ive been able to send...
5
2226
by: lanclot | last post by:
hi i am trying to authenticate a user using ClientLogin by sending a post to https://www.google.com/accounts/ClientLogin in my c program use ssl and socket the next code is my request:...
0
7118
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
6980
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
7157
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
7192
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...
1
6862
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...
1
4886
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...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1397
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 ...
0
282
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...

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.