2014-10-08 44 views
0

我在「app/var/assets」中有一些圖像(我必須將這些圖像放在該目錄中,我無法通過客戶端限制來更改此圖像)。使用X-Send文件顯示圖像(Symfony2)

我需要顯示這些圖像。 App目錄不是Apache的可訪問路徑,所以我需要使用X-Send File。

如何爲此使用X發送文件?

我想在我的控制器:

$path = $this->get('kernel')->getRootDir() . '/var/assets/example.jpg'; 

$response = new BinaryFileResponse($path'); 

$response->trustXSendfileTypeHeader(); 
$response->headers->set('Content-type', 'image/jpg'); 
$response->sendHeaders(); 
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, "name"); 

然後在我的觀點:

<img src="<?= $response ?>" /> 

但圖像沒有發現和網址我得到的圖像是:

HTTP/1.0 200 OKCache-Control:publicContent-Disposition:inline; filename =

有什麼想法嗎?

回答

0

我有一個類似的問題,我創建了一個操作,返回一個圖像。

public function getImgAction(){ 
    $f = fopen('/var/assets/example.jpg', "rb"); 
    $str = stream_get_contents($f); 
    fclose($f); 
    $response = new Response($str, 200); 
    $response->headers->set('Content-Type', 'image/jpg'); 
    return $response; 
} 
+0

謝謝,但是如何在您的視圖中打印此圖像?謝謝! – user2794692 2014-10-08 14:27:02

0

確保已安裝並啓用mod_xsendfile for apache。

在將變量傳遞給類構造函數時存在第3行中的字符串分隔符(應爲$response = new BinaryFileResponse($path);,但我認爲您知道這一點)。