Question

I have a php script that provides a link to a temporary file that is created with the script. I want the person to be able to download the file, but I don't want the file to stay on the server for a long time. I would like to remove the file say maybe after 2 minutes. How can this be done?

Was it helpful?

Solution

You can remove it right after the download. output the content of the file, then close it and unlink it.

Edit: example

$fo = fopen($f, 'rb') ;
    $content = fread($fo, filesize($f)) ;
    fclose($fo) ;
}
// Stream the file to the client 
header("Content-Type: application/octet-stream"); 
header("Content-Length: " . strlen($archive)); 
header("Content-Disposition: attachment; filename=\"myfile.exe\""); 
echo $archive;
unlink($f);

OTHER TIPS

Set up a cron job to to run your cleaning script every few minutes. You could use the filemtime() function to see when a file was created, and delete it if the file is considered "old enough".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top