tags:

views:

572

answers:

2

Hello everyone! 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?

Thanks

+6  A: 

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);
streetpc
This sounds great, but how can I output the content of the file? I meant to say that the file that is to be downloaded is actually an executable (.exe) file that was the result of a C++ compilation. How can I give the user a exe file download?
QAH
Send the appropriate header `header('Content-Type: application/octet-stream');` before writing to the buffer.
St. John Johnson
QAH, what streetpc is saying is don't provide a link to the file. Instead your original script should produce the file and output it to the user with the above code. Or you can provide a link to a script such as the one above that outputs and removes the file.
bmb
+2  A: 

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".

zombat