如何在codeigniter中下载和删除文件

时间:2013-07-09 08:35:46

标签: codeigniter file-upload

我想从我的数据库中删除并下载上传文件 我已经上传并显示它现在我想下载并删除iupload的文件 这是我的控制器showupload.php

<?php class Showupload extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
{

    $this->load->model('showuploadmodel');
    $data['query']=$this->showuploadmodel->showfile(); 
    $this->load->view('showupload_view',$data);


}
 public function delete_upload($file_id) {
    $this->load->showupload('showuploadmodel');
    $data = $this->showuploadmodel->delete($file_id);
    $this->load->view('showupload_view',$data);
}
public function download($filelink)
{
$this->load->helper('download');
$data = file_get_contents("$filelink"); // Read the file's link
force_download($filelink, $data);            
}
}
?>

这是我的观点show_upload_view.php

<?php include_once('header.php'); ?>
<?php include_once('tnavbar.php'); ?>

<html>
<head>
<title>Show Upload</title>
</head>
<body>


<div class='well'>
<table border="0">
  <tr>
    <th><h1>File Name</h1></th>
    <th><h1>Date</h1></th>
    <th><h1>Action</h1></th>   
    </tr>
<?php
foreach($query as $row){
 echo "<tr>";
echo "<td><h2>". $row->filename ."</h2></td>";
    echo "<td><h2>". $row->date ."</h2></td>";
    echo "<td><h2>".Download|Delete."</h2></td>";
    //now how can i make download and delete from my database
    //my database contains filename,fileid,filelink     
  echo "</tr>";  
}
?>
</table>

</div>

</body>
</html>

现在如何从我的数据库中下载和删除 我的数据库包含filename,fileid,filelink

1 个答案:

答案 0 :(得分:0)

要下载,您应该使用Download Helper

https://www.codeigniter.com/user_guide/helpers/download_helper.html

$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name, $data);

为了删除你在File Helper中有删除功能

https://www.codeigniter.com/user_guide/helpers/file_helper.html

delete_files('./path/to/directory/');