2014-11-05 102 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);