【发布时间】:2015-11-12 12:36:09
【问题描述】:
我有一个页面让用户下载服务器立即为他们压缩的数据。看起来是这样的:
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
问题:如果用户下载文件,清理工作正常。但是,如果用户取消下载,列表仍然未清理!
其他帖子的失败解决方案:其他帖子建议了此解决方案:
ignore_user_abort(true);
虽然这可以很好地清理,但它引入了一个新问题:如果用户取消,压缩过程将继续。这无缘无故地浪费了计算机上的资源。
如何保证清理运行?
【问题讨论】:
-
也许改变你的清理方式或
register_shutdown_functionphp.net/manual/en/function.register-shutdown-function.php -
@BrainFooLong 试试看!谢谢。
-
@BrainFooLong
register_shutdown_function()有效 :) 发布答案,以便我为您接受!
标签: php process stream resource-cleanup