Error downloading a file from my server with codeigniter

0

I am working on a project in codeigniter 3 , the problem I have is when downloading the file, when uploading the file to the server, I keep the path and the database in the database. name generated by the function do_upload of codeigniter , when I download it download it well but without content, either for word excel documents like pdf, when trying to open these files it shows them empty and in the pdf case does not open it, when viewing the documents on the server they are all complete with their content.

At the time of sending the file, I sent it via ajax using FormData and send it correctly.

Here's the upload code:

public function document(){
    $config['allowed_types']    = 'docx|xlsx|pdf';
    $config['upload_path']      = './assets/archivos/doc/';
    $config['remove_spaces']    = TRUE;
    $config['max_size']         = '20048';
    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('file')){
        $error = $this->upload->display_errors();
        $result = array('error' => true, 'mens' => $error, 'estado' => 3);
        echo json_encode($result);
    }else{
        $data = array(
            'nom_doc'  => $this->upload->data('file_name'), 
            'doc_size' => $this->upload->data('file_size'),
            'type_doc' => $this->upload->data('file_type'),
            'ruta_doc' => $this->upload->data('full_path'),
            'id_conf_sist' => 1
        );
        $res = $this->Config_model->insertDocument($data);
        if($res){
            $result = array('error' => false, 'mens' => 'El archivo '.$this->upload->data('raw_name').', a sido guardado correctamente.', 'estado' => 1);
            echo json_encode($result);
        }
    }
}

This would be the code to download:

public function getDownloadFile($name){
    $data = file_get_contents($this->folder.$name);
    force_download($name,$data);
}

Someone could tell me what would be the error for which I do not download the files well.

    
asked by Jhonny Luis 01.05.2018 в 00:13
source

1 answer

1

The error that occurs is that you are using the force_download method incorrectly, since in the official documentation it says that to download an existing file on the server you must use the method as follows:

public function getDownloadFile($name){
    //El primer parámetro es la locación del archivo y el segundo un NULL.
    force_download($this->folder.$name, NULL);
}

I leave the official documentation for more information:

  

link

    
answered by 01.05.2018 / 00:34
source