从数据库下载文件或在codeigniter中保存文件夹

时间:2015-10-19 17:41:13

标签: codeigniter

$data = 'Here is some text!';
$name = 'mytext.txt';

force_download($name, $data);

这是一个简单的下载代码位,我需要从数据库或我的保存路径文件夹下载。

我需要MVC codeigniter框架中的完整代码 我上传了一个数据库中的文件和一个给定的路径文件夹我做了我的最终学位的在线工作门户项目我想下载一个简历或文件,但我不能它它plz帮助我。我可以从数据库下载文件或保存文件夹。

并且此代码不起作用plz使用模型视图控制器向我发送完整代码。

function download($fid){
    $query= $this->db->get_where('files',array('fid' => $fid));

$row = $query->result();
header("Content-Type: ". $row[0]->type);
header("Content-Length: ". $row[0]->size);
header("Content-Disposition: attachment; filename=". $row[0]->name);

// Print data
echo $row[0]->content;

//Free the mysql resources
mysql_free_result($result);
//redirect('index.php/files/search/'.$fid);
 }

2 个答案:

答案 0 :(得分:1)

好的兄弟可以在下面看一下我的代码。

这是我的代码:

查看

<a href="<?php echo base_url(')?>assets/save_files/$s->file_name">Download</a>

首先制作一个foreach()语句,如foreach($result as $s)

<强>解释

$result你必须在你的控制器中创建一个函数:

<强>控制器

public function get_all_data()
{
 $data['result'] = $this->model->get_data(); // 
$this->load->view('index',$data); //past the data to the view
}

<强>模型

public function get_data()
{
$query = $this->db->get('table_name');
return $query->result();
}

然后使用上面的锚码下载保存文件的文件。

请注意:

  • save_files是文件夹。
  • $ S-&GT;是文件名。

答案 1 :(得分:1)

控制器:

public function download($file)
{

$this->load->helper('download');
$name = $file;
$data = file_get_contents('./uploads/foldername/'.$file); 
force_download($name, $data); 
    redirect('index','refresh');
}