dstk51319 2015-11-12 12:36
浏览 50
已采纳

php:用户取消后的清理过程残留

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?

  • 写回答

2条回答 默认 最新

  • dongyi8795 2015-11-12 14:11
    关注

    This should run everytime, even after aborting by user -> register_shutdown_function http://php.net/manual/en/function.register-shutdown-function.php

    // register a shutdown cleanup
    register_shutdown_function('doClean');
    
    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效