Delete file after uploading to codeigniter

I want to delete a file from the download directory after user download. But he does not leave. I am using Codeigniter download_helper to download a file. Below is my code:

controller

public function download($key,$id)
{
    $link=$this->article->download($key,$id);//get record from database via article model
    if(empty($link))
    {
        show_404(); 
    }
    force_download(DOWNLOADS_PATH.$link->file_name, NULL);//download file
    $this->article_lib->remove_downloaded($link->file_name);//user has downloaded so now delete this
}

Library

public function remove_downloaded($file_name)
{
   if(file_exists(DOWNLOADS_PATH.$file_name))
     {
      unlink(DOWNLOADS_PATH.$file_name);
     }
}
+4
source share
6 answers

Sorry I didn’t read, but the problem with your code is that it has no closure )

    public function remove_downloaded($file_name)
    {
       if(file_exists($file_name)) // here is the problem
         {
          unlink($file_name);
         }
    }

Also, how power loading works, everything that does not start. I would suggest using an ajax call after the controller.

UPDATE:

But, as you mentioned in your comment, you can delete the file before creating it, as indicated in this Unlink post after forcing a download that does not work Codeignign

+1
source

ignore_user_abort(true) (docs), , .

,

public function download($key, $id)
{
    $link = $this->article->download($key, $id);//get record from database via article model
    if (empty($link)) {
        show_404();
    }

    force_download(DOWNLOADS_PATH . $link->file_name, NULL);//download file

    ignore_user_abort(true); // Set whether a client disconnect should abort script execution
    if (connection_aborted()) {
        $this->article_lib->remove_downloaded($link->file_name);//user has downloaded so now delete this
    }
}
+1

, system/helpers/download_helper.php, , , force_download(), .

//File:  system/helpers/download_helper.php
// From line 135
// Generate the server headers
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$filesize);
header('Cache-Control: private, no-transform, no-store, must-revalidate');

// If we have raw data - just dump it
if ($data !== NULL)
{
    exit($data);
}

// Flush 1MB chunks of data
while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE)
{
    echo $data;
}

fclose($fp);
exit;
}
+1

, , . .

function rrmdir($dir) {
if (is_dir($dir)) {
    $files=scandir($dir);
    foreach ($files as $file)
     if ($file != "." && $file != "..") rrmdir("$dir/$file");
    rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}
0

, , , .

, , :

public function download($key,$id)
{
    $link=$this->article->download($key,$id);//get record from database via article model
    if(empty($link))
    {
        show_404(); 
    }
    force_download(DOWNLOADS_PATH.$link->file_name, NULL);//download file
    $this->article_lib->remove_downloaded(DOWNLOADS_PATH.$link->file_name); //Included full path
}

, .

0

:

$this->article_lib->remove_downloaded($link->file_name);

$this->article_lib->remove_downloaded(DOWNLOADS_PATH.$link->file_name);

, ,

$this->article->remove_downloaded(DOWNLOADS_PATH.$link->file_name);
0

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


All Articles