2016-01-20 80 views
0

我正在努力使用cakephp來實現具有圖像的CRUD。我設法上傳圖像,保存它(在maria/mysql數據庫中),現在我可以在phpmyadmin中看到上傳的圖像。不能使用cakephp顯示數據庫中的圖像

現在我試圖在瀏覽器中顯示一些圖像。在我MapasController我有以下方法:

public function mostrarImagem($id){ 
     $file = $this->Mapas->get($id); 
     $this->response->body($file->arquivo->uri); 
     $this->response->type('image/png'); 
     //$this->response->download('filename_for_download.png'); 
     return $this->response; 
    } 

但在瀏覽器中呼籲這給我的信息「圖像‘http://localhost:8765/Mapas/mostrarImagem/5’無法顯示,因爲它包含錯誤」。如果我取消$ this-> response->下載行的註釋,我會下載一個無效文件。 轉儲文件$變量,我得到以下內容:

Mapa {#147 ▼ 
    +"id": 5 
    +"titulo": "blablabla" 
    +"arquivo": stream resource @8 ▶} 
    +"created": Time {#145 ▶} 
    +"modified": Time {#146 ▶} 
    +"sistema_id": 5 
    +"unidade_id": null 
    +"[new]": false 
    +"[accessible]": array:1 [▶] 
    +"[dirty]": [] 
    +"[original]": [] 
    +"[virtual]": [] 
    +"[errors]": [] 
    +"[repository]": "Mapas" 
} 

爲$文件 - 內容> arquivo是:

+"arquivo": stream resource @6 ▼ 
    wrapper_type: "RFC2397" 
    stream_type: "RFC2397" 
    mode: "rb" 
    unread_bytes: 0 
    seekable: true 
    uri:  "data:text/plain;base64,iVBORw0KGgoAAAANSUhEUgAAA08AAAGnCAYAAABvtmzcAAAABmJLR0Q(....) 
mediatype: "text/plain" 
base64: true 
options: [] 

我在做什麼錯?

回答

2

數據URI不是一個有效的PNG圖像源,並且您不會以任何方式獲取數據URI,因爲在流上沒有這樣的屬性,您在轉儲中查看的內容只是調試信息元數據,所以這是預期的行爲。

您想要取回原始數據。

$this->response->body(stream_get_contents($file->arquivo, -1, 0)); 

...例如。

又見