当我从我的数据库表(用户)下载一个文件时,该文件将变得不可读。图像文件将失效,PDF 文件将无法加载。下载文件的大小也将变为 1 KB。
虽然一开始我已经成功地将文件上传到同一个表(用户),所以文件肯定不是 1kb。
我的控制器(Files.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Files extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('user');
}
public function index(){
$data = array();
//get files from database
$data['files'] = $this->user->getRows();
//load the view
$this->load->view('r_down', $data); //modell->view
}
public function download($id){
if(!empty($id)){
//load download helper
$this->load->helper('download');
//get file info from database
$fileInfo = $this->user->getRows(array('id' => $id));
//file path
$file = 'uploads/files/'.$fileInfo['picture'];
//download file
force_download($file, '$data');//name file nk save
//force_download($file,NULL);//name file nk save
}
}
我的观点(r_down.php)
<?php if(!empty($files)){ foreach($files as $frow){ ?>
<div class="file-box">
<div class="box-content">
<h5><?php echo $frow['name']; ?></h5>
<div class="preview">
<embed src="<?php echo base_url().'uploads/images/'.$frow['picture']; ?>"> <!-- nk view apa letak dlm %frow, ikut nama column. directory ikut folder pc?-->
</div>
<a href="<?php echo base_url().'index.php/files/download/'.$frow['id']; ?>" class="dwn">Download</a>
</div>
</div>
<?php } } ?>
模型(文件.php)
function getRows($params = array()){
$this->db->select('*');
$this->db->from('users');
$this->db->where('status','1');
$this->db->order_by('created','email');
if(array_key_exists('id',$params) && !empty($params['id'])){
$this->db->where('id',$params['id']);
//get records
$query = $this->db->get();
$result = ($query->num_rows() > 0)?$query->row_array():FALSE;
}else{
//set start and limit
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}
//get records
$query = $this->db->get();
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
}
//return fetched data
return $result;
}