Unlink after force download does not work Codeigniter

I was unable to delete the file after loading the force. 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);

}     

Please update me in this regard.

+1
source share
4 answers

I faced the same situation. So I just want to share the following information if it helps people in need.

Actually force_download ("file name", $ filepath);
Nothing that you wrote after this piece of code is executed because the force_download method has a header and an exit call .

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

, . force_download . , .

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

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

    } 
0

Use unlink ('filename') ;, you will not use the file path only to use the file name.

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

Source: https://habr.com/ru/post/1693836/


All Articles