我想在用户下载后从下载目录中删除我的文件。但它并没有被删除。我正在使用 Codeigniter download_helper 下载文件。 下面是我的代码:
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
}
图书馆
public function remove_downloaded($file_name)
{
if(file_exists(DOWNLOADS_PATH.$file_name))
{
unlink(DOWNLOADS_PATH.$file_name);
}
}
最佳答案
你可以在 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;
}
关于php - 在 codeigniter 中下载后删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48869189/