Using the Codeigniter Helper (Download_helper)

Asked

Viewed 198 times

0

Good evening! I’m using the function force_download() to download a file. However, I need to do this same procedure, but with downloading several files at the same time. I implemented a code, the logic I used is not performing such a procedure. Can anyone give me a hint of what I can do?

Controller:

 public function download($id = NULL){
    $this->load->helper('download');
    $download = $this->db->query('SELECT * FROM arquivos WHERE protocolo_id ='.$id);
    foreach ($download->result() as $itens){
        $diretorio = file_get_contents('./uploads/'.$itens->arquivo);         
        $arquivo = $itens->arquivo;
        force_download($arquivo, $diretorio);      
    }  
}        

1 answer

1


What I recommend to you is to use the library Zip to add your files in a compressed and then download them: Below its function similarly, which, I believe meet your need:

public function download($id = NULL){
    $this->load->library('zip');

    $download = $this->db->query('SELECT * FROM arquivos WHERE protocolo_id ='.$id);

    foreach ($download->result() as $itens){
        $this->zip->add_data('./uploads/'.$itens->arquivo, file_get_contents('./uploads/'.$itens->arquivo));
    }

    if($download->num_rows() > 0){
        $this->zip->archive('/path/arquivos.zip');
        $this->zip->download('arquivos.zip');
    }
}
  • Theoretically it worked, only when you extracted the zip file, it did not display the images. says: "Windows Photo Preview cannot open this image because it does not support this format..." Problem only in the files I download. Have some configuration to do?

  • It worked, yeah, I just changed the position of file_get_contents as second parameter. For the first is the file name and the second is the file content. Tanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.