Codeigniter - downloading file with use of force_d

2019-06-24 06:21发布

问题:

I'm using Latest CI. I have not any problem while working in local. But when i move my works to live server, i'm facing a problem.

When i download files from my download tab, File is getting download with correct size and format. But when i open that downloaded file, for ex, if it is an image, image is not displaying , or if it is word, it's asking for selecting encode type and after selecting encode type , content is junk characters.

How to solve this issue.?

Thanks in advance.

Code I used to download file:

$content = file_get_contents($file_loc);
force_download(FILENAME.EXT, $content);

回答1:

This works for me.

ob_clean(); 
$data = file_get_contents("localhost/qlip/uploads/filename.jpg"); //assuming my file is on localhost
$name = 'document.jpg'; 
force_download($name,$data); 


回答2:

try this code follow:

<?php

function downloadFile($file){
   $file_name = $file;
   $mime = 'application/force-download';
   header('Pragma: public');    
   header('Expires: 0');        
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Cache-Control: private',false);
   header('Content-Type: '.$mime);
   header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
   header('Content-Transfer-Encoding: binary');
   header('Connection: close');
   readfile($file_name);    
   exit();
}
?>

and call function :

<?php
downloadFile("./files/image.jpg");
?>

provided that the file "image.jpg" is the correct address



回答3:

If you prefer, you could try this:

function download() 
{
    // asumming you have http://www.domain.com/index.php/controller/download/file_name/extension/
    $extension = $this->uri->segment(4); // file extension
    $file_name = $this->uri->segment(3) . '.' . $extension; // file name
    $file_path = FCPATH . 'application/documentos/' . $file_name; // absolute path to file
    if (is_file($file_path)) {
        $mime = 'application/force-download';
        header('Pragma: public');    
        header('Expires: 0');        
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private',false);
        header('Content-Type: ' . $mime);
        header('Content-Disposition: attachment; filename="' . $file_name . '"'); file name
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');
        readfile(base_url() . 'application/documentos/' . $file_name); // relative path to file   
        exit();
    } else {
        redirect('/welcome/');
    }
}   


回答4:

Add ob_clean(); before the code of force_download(); else the image,word file get corrupted.

ob_clean();
$content = file_get_contents($file_loc);
force_download(FILENAME.EXT, $content);


回答5:

in CI 3 : works for me

 $data = 'Here is some text!';
 $name = 'mytext.txt';
 force_download($name, $data);

or :

 // Contents of photo.jpg will be automatically read
 force_download('/path/to/photo.jpg', NULL);

source



回答6:

your file path is incorrect. fix path to correct and run.it should work.