php - Accessing response (output) headers in ZF2 -
trying output image protected area using zf2. in action function output image data call to:
readfile($path);
in browser output recognized text , displayed starting jpeg markers 'ÿØÿà...bla bla bla'. i'm assuming because zend returned 'text' content type in http header.
so tried change content type header php command
header('content-type: image/jpeg');
when following error:
warning: cannot modify header information - headers sent (output started @ /zend/module/application/src/application/controller/imagescontroller.php:84)
i looked extensively , found lots of documentation this seems can like:
$this->getresponse()->setheader('content-type', 'image/jpeg');
when message:
fatal error: call undefined method zend\http\phpenvironment\response::setheader() in /zend/module/application/src/application/controller/imagescontroller.php on line 39
looking through response object structure don't see link between zend\http\headers , zend\http\response object or zend\http\phpenvironment\response object not surprising php can't find call setheader().
my question how change output header make borwser expect image data?
here code in response first answer. doesn't use view since outputting binary data:
class imagescontroller extends abstractactioncontroller { public function getfileaction() { $path = '/data/images/theimage.jpg'; header('content-type: image/png'); //$this->getresponse()->setheader('content-type:', 'image/jpeg'); readfile($path); exit; }}
in response saeven's reply here exact implementation of solution:
namespace application\controller; use zend\mvc\controller\abstractactioncontroller; use zend\session\container; use zend\view\model\viewmodel; use zend\http\response; use zend\http\headers headers; class imagescontroller extends abstractactioncontroller { public function getfileaction() { $table = $this->getevent()->getroutematch()->getparam('table'); $filename = $this->getevent()->getroutematch()->getparam('filename'); $path = '/datasm/apps/php/data/'.$table.'/'.$filename; $imagedata = file_get_contents($path); $response = $this->getresponse(); $response->setcontent($imagedata); $response->getheaders()->addheaderline('content-type', 'image/png'); return $response; } }
here screen cap of project showing don't have corresponding view file getfileaction()
here screen cap of how testing using browser , coming zend.
regarding version of zend using 2.2.5.
updated:
$response ->getheaders() ->addheaderline('content-transfer-encoding', 'binary') ->addheaderline('content-type', 'image/png') ->addheaderline('content-length', mb_strlen($imagecontent));
original answer :
this used xml file :
$response = new \zend\http\response(); $response->getheaders()->addheaderline('content-type', 'text/xml; charset=utf-8'); $response->setcontent($xml); return $response;
Comments
Post a Comment