Use ob_start to avoid creating zip files and get their content

92 views Asked by At

I have this code that creates a ".zip" file and inside it a ".xml" file obtained from a string. As seen in the example later I get your information and convert it to base64 and hash. The code is functional. What I want now is to use "ob_start()" so as not to have to create the ".zip" file, I don't know if someone could help me with a basic example, greetings...

<?php
$content = '<?xml version="1.0"?><Catalog><Book id="bk101"><Author>Garghentini, Davide</Author><Title>XML Developers Guide</Title><Genre>Computer</Genre><Price>44.95</Price><PublishDate>2000-10-01</PublishDate><Description>An in-depth look at creating applicationswith XML.</Description></Book><Book id="bk102"><Author>Garcia, Debra</Author><Title>Midnight Rain</Title><Genre>Fantasy</Genre><Price>5.95</Price><PublishDate>2000-12-16</PublishDate><Description>A former architect battles corporate zombies,an evil sorceress, and her own childhood to become queenof the world.</Description></Book></Catalog>';
$route = './temp/';
$name  = 'facturaElectronicaCompraVenta.xml.zip';
$file  = "{$route}{$name}";

// CREATE ZIP
$zp   = gzopen($file,'w9');
gzwrite($zp,$content);
gzclose($zp);
// READ ZIP
$fp     = fopen($file,'rb');
$binary = fread($fp,filesize($file));

$res = [
    'archivo'     => base64_encode($binary),
    'hashArchivo' => hash('sha256',$binary),
];
print_r($res);
1

There are 1 answers

0
Álvaro González On

First of all, the output buffer (ob...) functions don't accomplish anything related to files, they only capture the script output (e.g., echo 'Hello, World!).

If you want to keep using gzopen(), perhaps you can just provide a stream wrapper pointing to anything that isn't a physical file (I haven't investigated that option) but it looks easier to just switch to gzencode().