You are here: Re: A follow-up to my download-all-files-as-a-zip-file post « PHP Programming Language « IT news, forums, messages
Re: A follow-up to my download-all-files-as-a-zip-file post

Posted by Matt Madrid on 08/05/07 09:02

techusky@gmail.com wrote:
> On Aug 4, 8:04 pm, Matt Madrid <admiral...@gmail.com> wrote:
>> 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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация