views:

127

answers:

6
            function new_photo()
            {

                if( !empty($this->data))
                {
                        $this->data['Photo']['showcase_id'] = $this->Session->read('CurrShowcase.id');
                        $this->data['Photo']['added'] = date("Y-m-d H:i:s");
                        $this->Showcase->Photo->save($this->data);

                        $flasher = 'Photo uploaded successfully';
                        $flasher .= '<br/><img src="' . $this->data['Photo']['thumbnail_url'] . '"/>';
                        $this->Session->setFlash($flasher);
                        //$this->redirect(array('action'=>'sc',));
                }
            }   

I have a Showcase Controller in my CakePHP app, and a new photo form to submit new photos. Whenever I uncomment the last line that redirects after the data is saved, I get this error:

Warning (2): Cannot modify header information - headers already
sent by (output started at D:\.....

Even if I get this error, $this->data still gets saved properly in the database. However, if I comment the redirect line as shown above, everything works fine and error-free. I HAVE checked for blank spaces around the tags, so I'm pretty sure it's not that.

Any ideas?

Edit: commenting out the setFlash statement does not fix the problem.

A: 

I'm assuming setFlash outputs something to the browser?

If whitespace before or after your <?php ?> tags isn't your issue you might have to try passing 'null' for the 'layout' parameter of setFlash();

i.e.

$this->Session->setFlash($flasher, null); 
Graphain
Yes it just flashes a message (in this case, $flasher) on the next page loaded
vette982
If you comment out setFlash and then redirect does it work?
Graphain
Answer updated with a possible solution.
Graphain
If I comment out setFlash it still does the same thing.
vette982
Then it's probably outside the code we can see. Either you have an invisible character (try copying the code within your PHP tags into a new file with new php tags and using a new text editor) or you are calling another function that is sending either text or a header.
Graphain
Actually it tells you what file is sending the output anyway right? Is it the same file?
Graphain
A: 

Either this code outputs something to the browser, or you have a whitespace after ?> in the end of the file (or any other included file). The whitespace is sent to the user thus sending http header.

cypher
A: 

Is there a debug statement somewhere that you're not showing us?

Leo
A: 

You may be up against an invisible UTF-8 BOM character somewhere. Check your text editor settings whether it saves your files with BOM or without.

deceze
A: 

Change your debug mode to 0 to make sure it's not a notice/warning being generated prior to the redirect. Also, you might want to tighten up your processing section to (be paranoid and) ensure that it's not using invalid indexes, as well as anywhere else throughout the application flow for that action to make sure you're not getting any ouput (if it works when you change debug to 0).

mway
A: 

I'd check for whitespace in the models. Anyone of them. That was one of the gotchas I hit.

Nigel