簡體   English   中英

強制下載失敗后斷開鏈接Codeigniter

[英]Unlink after force download not working Codeigniter

強制下載后我無法刪除我的文件。下面是代碼

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

}     

請就此向我介紹。

我遇到過同樣的情況。 因此,如果有需要的人,我只想分享以下信息。

實際上是force_download(“ file-name”,$ filepath);
在這段代碼之后,您沒有編寫任何內容,因為force_download方法具有標頭退出調用。

因此,如果要刪除正在下載的文件,則可以在調用force_download方法之前將其刪除。

有人會懷疑我們在下載文件之前如何刪除它。實際上,force_download方法中的第二個參數實際上是正在下載的文件的內容。 一旦有了該文件,就不需要該文件。

$file_content = file_get_contents($file_path); // Read the file's contents
if(file_exists($file_path)){
    unlink($file_path);
}
force_download($filename, $file_content);

使用base_url()路徑下載文件,並使用FCPATH取消鏈接文件(文件的絕對路徑)

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

    } 

使用unlink('filename');,您將不會僅使用filename來使用文件路徑。

$file_content = file_get_contents($file_path); // Read the file's contents
if(file_exists($file_path)){
    unlink($file_path);
}
force_download($filename, $file_content);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM