Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrupted ZipArchive after setContent() in php symfony
    text
    copied!<p>I have a real strange behavior when sending a zip archive containing a any type of file in symfony. The problem is that the zip file, that I download from the browser contains an extra 'bit' at the beginning of the file.</p> <p>Here's my code:</p> <pre><code>$tmpFileName = tempnam("/tmp", "xb_"); $zip = new ZipArchive(); $zip-&gt;open($tmpFileName, ZipArchive::CREATE); $zip-&gt;addFile('[directory_inside_webspace]/test.pdf', 'myTest.pdf'); $zip-&gt;close(); $this-&gt;getResponse()-&gt;clearHttpHeaders(); $this-&gt;getResponse()-&gt;setContent(file_get_contents($tmpFileName)); $this-&gt;getResponse()-&gt;setHttpHeader('Content-Type', 'application/zip'); $this-&gt;getResponse()-&gt;setHttpHeader('Content-Disposition', 'attachment; filename=archive.zip'); return sfView::NONE; </code></pre> <p>Now here's what is strange. The temporary zip file under /tmp is fine. I can extract it without any problems. But the file the browser sends to me is corrupt. When I open both of them in hex editor, the first few bytes look like:</p> <pre><code>working file: 50 4B 03 04 14 00 00 corrupt file: 0A 50 4B 03 04 14 00 00 </code></pre> <p>When I remove the addition '0A' from the corrupt file I can open it without problems. It's now the exactly same file as the tmp file created in /tmp.</p> <p>Did anyone had a behavior like this before?? I'm stucked at this problem for over 4 days now and I'm not able to find the error. I use exactly the same code in another symfony module and it's working in there. Any ideas ?</p> <p>-----Update------</p> <p>The problem wasn't related to the zipArchive or setContent. I had a query at the beginning of the function.</p> <pre><code>$bill = Doctrine::getTable('Bill')-&gt;find($request-&gt;getParameter('id')); </code></pre> <p>In the class 'Bill.class.php' in my doctrine model I had a blank line before </p> <pre><code>&lt;?php </code></pre> <p>So when the class Bill was instantiated the new line was print to screen.</p> <p>I just wonder why php doesn't give an error message like 'headers already send' when sending the zip Archive to the browser.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload