I have a page where I get the user to download data that the server zips right away for them. This is how it looks:
createFilesList(); // <---- creates a text list of files do be zipped
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="'.$downloadFilename.'');
$fp = popen('cat '.$fullListOfFiles.' | sudo -u myuser zip -@ -9 - ', 'r');
$bufsize = 8192;
$buff = '';
while( !feof($fp) )
{
$buff = fread($fp, $bufsize);
echo $buff;
}
pclose($fp);
doClean(); // <----- deletes the list of files
The problem: If the user downloads the file, the clean-up works fine. However, if the user cancels the download, the list remains there uncleaned!
Failed solution from other posts: Other posts have suggested this solution:
ignore_user_abort(true);
While this works fine to cleanup, it introduces a new problem: If the user cancels, the zipping process continues. This wastes resources on the computer for no good reason.
How can I guarantee that the clean-up runs?