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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to