php - 从 phpmyadmin 下载的 Codeigniter 文件变得不可读
问题描述
当我从我的数据库表(用户)下载一个文件时,该文件将变得不可读。图像文件将失效,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;
}
解决方案
你的download
方法应该是这样的:
public function download($id)
{
if(!empty($id))
{
$this->load->helper('download');
$fileInfo = $this->user->getRows(array('id' => $id));
$file = FCPATH.'uploads/files/'.$fileInfo['picture'];
force_download($file, NULL);
}
}
你的观点应该是这样的:
<?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="<?=site_url('uploads/images/'.$frow['picture']); ?>">
</div>
<a href="<?=site_url('files/download/'.$frow['id']);?>" class="dwn">Download</a>
</div>
</div>
<?php } } ?>
更多信息:https ://www.codeigniter.com/user_guide/helpers/download_helper.html
推荐阅读
- angular - Angular 7 - 防止在提交时删除其他字段数据
- java - 使用 Java 版本 11 或更高版本构建时 kaptDebugKotlin 崩溃
- android - 在 react-native android 项目中,如何将文件夹从资产复制到 DocumentDirectoryPath?
- git - 如何将忽略的子目录推送到另一个遥控器?
- c++ - 用于替换现有文件的 MoveFile() 在 Release 上不起作用,但在 Debug (C++) 上工作正常
- python - python-更新日志内容时动态更改日志目录
- javascript - 使用参数类型 U 的 T 获取类型 T 的函数的参数类型
- java - 两次调用返回静态对象的静态方法时只调用一次构造函数?
- visual-studio-code - 更新 OmniSharp 扩展
- django-templates - 我们可以在 Django 框架的 scriptlet 中使用多个呈现的查询集和多个 for 循环吗?