Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove File After Time in PHP

Tags:

php

exe

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?

like image 283
QAH Avatar asked Sep 11 '25 02:09

QAH


2 Answers

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);
like image 177
instanceof me Avatar answered Sep 13 '25 14:09

instanceof me


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

like image 27
zombat Avatar answered Sep 13 '25 15:09

zombat