2016-08-22 21 views
0

我是苗條框架非常新,我想下載一個圖像文件。在本地使用php工作良好。但是,當在服務器上應用相同的代碼以使其不起作用時。搜索了很多,但沒有運氣,例如herethis one。這裏是我的代碼內容類型:圖像/ jpeg不在當地工作

$app->get('/image', function ($request, $response, $args) { 
//header('Content-type: image/png'); 
//header('Content-Disposition: attachment; filename=go-logo.jpg'); 
//$contents = file_get_contents(getcwd() . '/' . $_GET['img']); 
$filename=getcwd() . '/' . $_GET['img']; 
header("Content-Length: " . filesize($filename)); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=go-logo.jpg'); 
readfile($filename); 
return $contents; 

}); 

enter image description here

錯誤是它顯示它是一個HTML文件。我想這樣的

enter image description here

+0

你是什麼意思不工作? '$ contents'也沒有在任何地方定義。 –

+0

對不起。我忘了附加screenshort – MKD

+0

請現在檢查 – MKD

回答

0

您需要設置響應對象的標頭。 然後將文件寫入Response主體。

return $response->withHeader("Content-Type", "application/octet-stream") 
     ->write($fileContents); 
+0

很好,它的工作。非常感謝 – MKD

相關問題