2015-11-07 86 views
0

我試圖下載上載到CakePHP中的文件。我可以下載它,但是它顯示PDF文件的'無法加載PDF'和文檔的'文件已損壞'。我不知道代碼中出了什麼問題。無法加載下載的內容(CakePHP)

這裏是簡要的代碼。

<?php 
header('Content-Length '.$requiredFile['Upload']['size']); 
header('Content-type: '.$requiredFile['Upload'['type']]); 
header('Content-Disposition: attachment; filename="'.$requiredFile['Upload']['name'].'"'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
echo $requiredFile['Upload']['content']; 
?> 

$ requiredFile是一個數組:

Array 
(
    [Upload] => Array 
     (
      [id] => 12 
      [tenant_id] => 29 
      [name] => Niraj Paudel (CV).pdf 
      [type] => application/pdf 
      [size] => 126626 
      [content] => 'content over here' 
) 
); 

我會很感激,如果你們幫我解決這個問題。

回答

1

在cakephp中,您可以簡單地下載文件發送迴應。假設您的文件已上傳到webroot/uploads/,以下是將下載文件的示例代碼。

return $this->response->file(WWW_ROOT.'uploads/yourfilename.pdf', array('download' => true, 'name' => 'yourfilename')); 
+0

其實我上傳了文件,取了它的名字,類型,大小,內容並保存到mysql數據庫表中。當我檢索數據時,我在$ requiredFile數組中有名稱,類型,大小和內容。在這種情況下,我怎麼能使用上面的代碼。先謝謝你。 –

+0

您可以在檢索數據的功能中使用此代碼。你在哪裏上傳你的文件? – Karan

+0

我已經從控制器操作'downloadFiles'發送$ requiredFile數組來查看'download_files.ctp'。在'download_files.ctp'中,我使用了上面提到的代碼。 –