单击服务器上的下载链接后如何删除下载的文件

问题描述 投票:0回答:3

我正在生成.MSI文件并提供下载链接。但是下载后我需要删除该文件(如果用户选择“保存”不取消)。请在这方面给我一些输入。

我想使用readfile()但它会通过输出到browser.which不是我的情况。我需要点击下载MSI文件。

<a href="http://localhost/john_CI/june/Enterprise-Setup_test.msi" id='32_bit_url1'>32-bit suite </a> 
javascript php file codeigniter
3个回答
0
投票

制作一个像这样的控制器功能:

public function download($id) {
        $this->load->helper('download');
        $filepath = "url/" . $id;
        force_download("file-name", $filepath);
        ignore_user_abort(true);
        unlink($filepath);
    }

0
投票

更好的尝试这样做。

                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment;  filename='.basename($file));
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
               // Ignore user aborts and allow the script to run                                        
                ignore_user_abort(true);

                //read the contents and store in memory
                readfile($file);

                //remove the file after download
                unlink($file);

0
投票

我不知道为什么你想在下载终止后立即删除文件,无论如何你可以更好地采用另一种方法来处理这类事情。为什么不将链接保存在数据库表中并给它一个过期日期?这样,您的用户可以下载文件,直到链接处于活动状态,您可以稍后轻松删除过期的文件。

© www.soinside.com 2019 - 2024. All rights reserved.