我如何根据文件ID使用CodeIgniter从数据库下载文件?

时间:2018-01-22 04:59:08

标签: php codeigniter

$this->load->helper('download');

//get file info from database
$id = $this->uri->segment(4);
$fileInfo['gambar'] = $this->mod_surat_masuk->download($id);

//download file from directory
$file = 'gambar_product'.$file['gambar'];
force_download($file, NULL);

我的代码但无论如何都没有用 那么如何根据文件ID使用Codeigniter进行下载功能?

2 个答案:

答案 0 :(得分:1)

CodeIgniter有一个很好的帮助功能来下载文件。

public function download ($file_path = ""){
    // load ci download helder
    $this->load->helper('download');  

    // get download file path and store it in $data array
    $data['download_file'] = $file_path;  

    // load view file    
    $this->load->view("download_view",$data);
    redirect(current_url(), "refresh");                       
}

在视图部分(download_view.php)中,只需调用下载函数即可。

if(! empty($download_file))
{
    $data = file_get_contents(base_url("/path/".$download_file)); 
    // Read the file's contents
    $name = $download_file;
    force_download($name, $data);
}

答案 1 :(得分:0)

试试这个:

<?php
$this->load->helper('download');
//get file info from database
$id = $this->uri->segment(4);
$fileInfo = $this->mod_surat_masuk->download($id);

$source_file = file_get_contents(base_url() . 'gambar_product/' . $fileInfo['NAME_OF_THE_FILE']);
$file_name = $fileInfo['NAME_OF_THE_FILE'];
ob_clean();
force_download($file_name, $source_file);
?>