ielanna
ielanna

Reputation: 1

download file from database codeigniter

here is a code snippet of my download function. it does download the file however there are times when i try to open the downloaded file, i get an error. it seemed the file is corrupted.. can somebody tell me what's wrong with the codes?

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);
}

Upvotes: 0

Views: 2377

Answers (2)

Réjôme
Réjôme

Reputation: 1484

If you are sure that your request gets 1 and only 1 row, try to use :

$query->row();

in place of

$query->result();

Upvotes: 0

Poelinca Dorin
Poelinca Dorin

Reputation: 9703

Check $row[0]->type, $row->[0]->size, $row[0]->name ( comment out all the header() calls, and dump $row ), you're allso not testing if $query->result() returned a valid result , allso check if there are any php warnings or notices, allso check if there are any headers allready sent to the browser before you call header("Content-Type...

Upvotes: 1

Related Questions