在codeigniter中下载后删除文件

问题描述 投票:4回答:6

我想在用户下载后从下载目录中删除我的文件。但它没有被删除。我正在使用Codeigniter download_helper下载文件。以下是我的代码:

调节器

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
}

图书馆

public function remove_downloaded($file_name)
{
   if(file_exists(DOWNLOADS_PATH.$file_name))
     {
      unlink(DOWNLOADS_PATH.$file_name);
     }
}
php file download codeigniter-3
6个回答
1
投票

对不起,我没有阅读,但你的代码的问题是它缺少关闭)

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

强制下载的工作方式也就是任何事情都不会运行。我建议在控制器之后使用ajax调用。

更新:

但正如您在评论中提到的,您可以在创建文件之前删除该文件,如本文Unlink after force download not working Codeigniter中所述


1
投票

我使用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;
}

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
投票

确保删除的文件不是目录。如果不确定那么试试这个。它也会删除文件和目录

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.