【问题标题】:Download file helper下载文件助手
【发布时间】:2015-09-01 04:36:42
【问题描述】:

我想下载我已经上传到我的网站的文件 我只是将文件名保存到数据库中

我的控制器

public function index()
{   
    $data['file'] = $this->home_model->file();
    $this->load->view('file', $data);
}

public function download()
{   

    $id_file = $this->uri->segment(3);
    $data = file_get_contents("/upload/kepsek/".$id_file);
    force_download('$id_file', $data);
}

我的模型

function file()
    {
        $query=$this->db->query("SELECT * FROM silabus");
        return $query->result();

    }

我的看法

<table class="table table-bordered text-center" id="table-user">
                    <thead>
                      <tr class="info">
                        <th>Download</th>
                        <th>Pelajaran</th>

                      </tr>
                    </thead>
                    <tbody>
                      <?php
                      foreach($file as $data){
                      ?>
                      <tr>
                        <td width="50%"><a href="<?php echo site_url('download/download/'.$data->file);?>">Download</a></td>
                        <td width="50%"><?php echo $data->pelajaran;?></td>
                        </tr>
                      <?php
                      }
                      ?>
                      </tbody>
                      <tfoot>
                      </tfoot>
                  </table>

当我点击下载时没有发生任何事情 有什么办法让它工作吗?

【问题讨论】:

标签: php codeigniter download helper


【解决方案1】:

试试这个

$data = file_get_contents("upload/kepsek/".$id_file);

同时检查“upload/kepsek/”.$id_file 文件是否存在

【讨论】:

    【解决方案2】:
    <table class="table table-bordered text-center" id="table-user">
        <thead>
        <tr class="info">
            <th>Download</th>
            <th>Pelajaran</th>
    
        </tr>
        </thead>
        <tbody>
        <?php
            foreach($file as $data){
                ?>
                <tr>
                    <td width="50%"><a href="<?php echo base_url().'index.php/download/download/'.$data['file'];?>">Download</a></td>
                    <td width="50%"><?php echo $data->pelajaran;?></td>
                </tr>
            <?php
            }
        ?>
        </tbody>
        <tfoot>
        </tfoot>
    </table>
    

    然后在下载控制器中

    public function index()
    {   
        $data['file'] = $this->home_model->get_file();
        $this->load->view('file', $data);
    }
    
    public function download($id)
    {   
        $this->load->helper('file');
        $this->load->helper('download');
    
        $data = file_get_contents("./upload/kepsek/".$id);  //check file exist
        $name = my_file.php//your file type
        force_download($data, $name);
    }
    

    在模型中

    function get_file()
    {
        $query= $this->db->query("SELECT * FROM silabus");
        $result =  $query->result_array();
        return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2014-11-14
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多