Unlink after force download not working Codeigniter

送分小仙女□ 提交于 2019-12-23 01:40:05

问题


i was unable to remove my file after force download.below is the code

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

}     

Kindly update me on this regard.


回答1:


I have came across same situation. So i just want to share the following information if that helps people in need.

Actually force_download("file-name", $filepath);
Nothing you have written after this piece of code will be executed, because force_download method has a header and an exit call.

So if you want to delete file which is being downloaded, you can delete it before calling force_download method.

Some people will be having a doubt like how can we delete a file before downloading it.Well actually the second parameter in force_download method is actually the content of the file being downloaded. Once you have that you do not require the file.

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



回答2:


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



回答3:


Use unlink('filename');,you will not use filepath only use filename.




回答4:


Use base_url() path for download the file and use FCPATH for unlink the file(absolute path to the file)

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

    } 


来源:https://stackoverflow.com/questions/31157589/unlink-after-force-download-not-working-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!