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条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛