[英]How to remove the downloaded file after clicking on the download link from server
我正在生成一個.MSI文件並提供了下載鏈接。但是下載后,我需要刪除該文件(如果用戶選擇“保存”而不是取消)。請為此提供一些信息。
我以為使用readfile(),但是它將通過輸出到瀏覽器。這不是我的情況。 我需要點擊下載MSI文件。
<a href="http://localhost/john_CI/june/Enterprise-Setup_test.msi" id='32_bit_url1'>32-bit suite </a>
使控制器功能像這樣:
public function download($id) {
$this->load->helper('download');
$filepath = "url/" . $id;
force_download("file-name", $filepath);
ignore_user_abort(true);
unlink($filepath);
}
最好嘗試這樣做。
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);
我不知道您為什么要在下載終止后立即刪除文件,無論如何,如果您可以采用另一種方法來處理這種事情,那將更好。 為什么不將鏈接保存在數據庫表中並給它一個過期日期? 這樣,您的用戶可以下載文件,直到鏈接處於活動狀態為止,之后您可以輕松地刪除過期的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.