Replace return sfView::NONE by throw new sfStopException;
On Mon, Mar 2, 2009 at 8:52 PM, Olivier LOYNET <[email protected]>wrote:
>
> Hi,
>
> I've a problem of < headers already sent > when I send a file to the
> browser.
>
> My config is :
> - CentOs 5.2 with php 5.2.9
> - symfony version 1.2.4 (stable)
>
>
> The code to show the problem, I've made this method :
>
> [code]
> public function executeFile(sfWebRequest $request)
> {
> $this->line = $request->getParameter('line', 1024);
> $this->path = sfConfig::get('sf_web_dir').'/tmp/';
> $this->filename = 'file.txt';
>
> // generate a text file of (n line of 1KB)
>
> $my1kb = str_repeat('0123456789abcdef', 64);
>
> $handle = fopen($this->path.$this->filename, "wb");
> for ($i=0; $i < $this->line; $i++)
> {
> fwrite($handle, $my1kb);
> }
> fclose($handle);
>
> // send the file to the browser
>
> $response = $this->getResponse();
> $response->clearHttpHeaders();
> $response->setContentType('text/plain; charset=UTF-8');
> $response->setHttpHeader('Content-Disposition', 'attachment;
> filename='.$this->filename);
> $response->setHttpHeader('Content-Length',
> filesize($this->path.$this->filename));
> $response->setContent(file_get_contents($this->path.$this->filename,
> false));
> $response->sendHttpHeaders();
> $response->sendContent();
>
> return sfView::NONE;
> }
> [/code]
>
> The result (file of 1MB) :
>
> [result]
>
> 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789ab
>
> cdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef01234567
> 89abcdef0123456789abcdef0123456789abcdef. . .
> . . . 3456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef<br />
> <b>Warning</b>: Cannot modify header information - headers already sent by
> (output started at
> ./lib/vendor/symfony/lib/response/sfResponse.class.php:105) in
> <b>./lib/vendor/symfony/lib/response/sfWebResponse.class.php</b> on line
> <b>335</b><br />
> <br /> .
> [/result]
>
> If I generate a file less then 5KB, it works fine !
> I test the same code on WinXP (XAMPP with php 5.2.4), it works fine !
>
> Where is the problem ?
>
> I've clean the cache
> I've made tests on prod en dev environnement
> There is no ?> at the end of the class
> I've upgrade php from 5.2.8 to 5.2.9 and it's the same result.
>
> Is PHP ? is symfony ? or else ?
>
> Olivier
>
>
> >
>
--
Thomas Rabaix
http://rabaix.net
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---