【问题标题】:How to remove the downloaded file after clicking on the download link from server单击服务器上的下载链接后如何删除下载的文件
【发布时间】:2015-06-30 10:34:01
【问题描述】:

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

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

<a href="http://localhost/john_CI/june/Enterprise-Setup_test.msi" id='32_bit_url1'>32-bit suite </a> 

【问题讨论】:

    标签: javascript php file codeigniter


    【解决方案1】:

    制作这样的控制器功能:

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

    【讨论】:

    • 在点击下载链接之前会直接取消文件链接
    • 首先你使用codeigniter download_helper。这将有助于强制下载。在该函数中,您可以编写 unlink 文件
    【解决方案2】:

    最好尝试这样做。

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-21
        • 2011-12-05
        • 1970-01-01
        • 2020-07-04
        • 2011-08-09
        • 1970-01-01
        • 2020-08-20
        相关资源
        最近更新 更多