You are here: Re: [PHP] Delete a file immediately after download « PHP « IT news, forums, messages
Re: [PHP] Delete a file immediately after download

Posted by Richard Lynch on 10/04/61 11:08

Shaun wrote:
> Thanks for the reply, I know how to delete a file I just wanted to know if
> its possible to ensure that its deleted automatically as soon as the user
> has downloaded it.

Yes.

You don't even put the file into your web tree DocumentRoot directory, and
you write a PHP script that allows the user to view it.

Something like this:

<?php
$path = "/path/to/some/directory/not/in/the/web/tree/";
$filename = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : die();
//Use this to be damn sure they don't go after /etc/passwd or ../secret
or...
$filename = preg_replace('/[^A-Za-z0-9_-]/', '', $filename);
//Allow for the possibility of the file not being read correctly, and
//only delete it if your hardware behaves.
if (readfile("$path$filename")){
unlink("$path$filename);
}
?>

The only thing is, though, that if the user aborts halfway through their
download, or their network connection flakes out, or, for that matter,
*YOUR* network connection dies on the server, or...

There are a lot of reasons why the file could end up not ACTUALLY being
delivered to the browser, but you'd still delete it.

If the user can just "do over" whatever they did to get the file, that's
no big deal.

If this is something where they paid money for it, but you delete the file
before they REALLY successfully get it, that's a bit more of a problem.

In that case, you might be better off to warn the user the file will be
available for download only for 72 hours, and record a time-stamp in the
db with each filename when it gets created, and then run a cron job that
unlink's anything from the db older than 72 hours.

Which you might want to do ANYWAY to get rid of files that some goofball
user never bothers to download, or the readfile actually does fail (rare,
but possible) or they cancel the connection and your script quits halfway
through the readfile or...

--
Like Music?
http://l-i-e.com/artists.htm

 

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

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