2014-11-05 69 views
0

我试图在php下载文件。我使用了codeigniter下载助手和纯php代码读取文件。我可以在浏览器的网络中获取文件的内容,但无法下载。Php/Codeigniter文件下载

当我试图在一个独立的PHP文件中的readfile函数工作。

这是我的dowload文件的代码块。

// codeigniter 
$this->load->helper('download'); 
$data = file_get_contents(base_url("/images/logo.png")); 
force_download($decodedFileInfo->fileName, $data); 

// php readfile 
$file = "./images/logo.png"; //. $decodedFileInfo->fileName; 

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    readfile($file); 
    exit; 
}else{ 
    var_dump("hst");die; 
} 

谢谢。

+0

'的file_get_contents(BASE_URL(...))'会做一个HTTP请求到自己的服务器。为什么?为什么你不能完全绕过http层并直接从文件系统获取它?这比FAR更有效率,比强制完整的http往返。 – 2014-11-05 21:22:10

回答

0

尝试在变量保存文件名和图像名称和回声在屏幕上。 我测试这个功能完美的作品:

$this->load->dbutil(); 

//Calling a store procedure 
$sp ="exec secondProc '5 Aug 2013'"; 
$query = $sqlsrvr->query($sp);//->result(); 
$jawad = $query->result_array(); 

//pass it to db utility function 
$new_report = $this->dbutil->csv_from_result($query); 

//Downloading the file 
$this->load->helper('download'); 
force_download('csv_file.csv', $new_report);