php - ZF2 Stream with byte array -


i byte array rest api. create local file byte array. , can send file zf2 browser.

code within zf2 controller action:

file_put_contents($fullpath, $rawpdf);  $headers = new \zend\http\headers();  $contentdisposition = ($view == 'inline') ? 'inline': 'attachment';  $headers->addheaders(array(     'content-disposition' => $contentdisposition . '; filename="' . basename($fullpath) . '"',     'content-type' => 'application/pdf',     'content-length' => filesize($fullpath),     'expires' => '@0',     'cache-control' => 'must-revalidate',     'pragma' => 'public'  )); $response = new \zend\http\response\stream(); $response->setstream(fopen($fullpath, 'r')); $response->setstreamname(basename($fullpath)); $response->setstatuscode(200); $response->setheaders($headers); return $response; 

but directly send byte array browser without creating local file.

how can set stream ($response->setstream()) byte array? how create resource byte array, without creating local file?

with plain old php can that:

$rawpdf = ''; array_walk($bytearray, function($value) use (&$rawpdf) {     $rawpdf .= chr($value); }); header('content-type: application/pdf'); header('content-disposition: inline; filename="bericht.pdf"'); header('content-transfer-encoding: binary'); header('accept-ranges: bytes'); echo $rawpdf; 

this simplified example of code use export data in csv file , download if automatically.

you can adapt fit want

header('content-type: text/csv'); header('content-disposition: attachment;filename="csv_file.csv'); header('cache-control: max-age=0');  $file = fopen('php://output', 'w');  foreach ($this->getdata() $data) {      fputcsv($file, $data); }  fclose($file); exit(0); 

Comments