从数据库codeigniter下载文件

时间:2011-03-14 01:28:01

标签: database file codeigniter download

这是我的下载功能的代码片段。它确实下载了文件,但有时我尝试打开下载的文件,我收到一个错误。它似乎文件已损坏..有人可以告诉我这些代码有什么问题吗?

function download($fid){
    $query= $this->db->get_where('files',array('fid' => $fid));

    $row = $query->result();
    header("Content-Type: ". $row[0]->type);
    header("Content-Length: ". $row[0]->size);
    header("Content-Disposition: attachment; filename=". $row[0]->name);

    // Print data
    echo $row[0]->content;

    //Free the mysql resources
    mysql_free_result($result);
    //redirect('index.php/files/search/'.$fid);
}

2 个答案:

答案 0 :(得分:1)

检查$row[0]->type, $row->[0]->size, $row[0]->name(注释掉所有的header()调用,然后转储$row),如果$query->result()返回了有效的结果,你也不测试,还要检查是否有任何php警告或通知,在你致电header("Content-Type之前检查是否有任何标题已经发送到浏览器......

答案 1 :(得分:0)

如果您确定您的请求只有1行,请尝试使用:

$query->row();

取代

$query->result();
相关问题