首页 > 解决方案 > 从 phpmyadmin 下载的 Codeigniter 文件变得不可读

问题描述

当我从我的数据库表(用户)下载一个文件时,该文件将变得不可读。图像文件将失效,PDF 文件将无法加载。下载文件的大小也将变为 1 KB。

虽然一开始我已经成功地将文件上传到同一个表(用户),所以文件肯定不是 1kb。

表(用户)

打开pdf文件时的错误示例

我的控制器(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;
    }

标签: phpcodeignitercodeigniter-3codeigniter-download

解决方案


你的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


推荐阅读