By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

Problem using the header function. Warning: Cannot modify header information

P: 10
hi there, i m using this coding to retrieve the file from database so that my user can download the file...

Expand|Select|Wrap|Line Numbers
  1.  <?php
  2.  
  3.       if (!isset($_GET['id'])) die('Invalid Parameter');
  4.  
  5.       include 'connectdb.php'; 
  6.  
  7.       $id = $_GET['id'];
  8.  
  9.       $result= "SELECT content FROM upload WHERE id = '$id'";
  10.  
  11.       $data = mysql_query($result) or die(mysql_error());   
  12.  
  13.       $info = mysql_fetch_array($data);
  14.  
  15.       header('Content-disposition: attachment; filename="'.$id.'"');
  16.  
  17.       header('Content-type: application/msword');
  18.  
  19.       //header('Content-type: text/plain');
  20.  
  21.       readfile($info['Content']);
  22.  
  23.       ?>
  24.  
result,i keep getting this warning..
Expand|Select|Wrap|Line Numbers
  1. Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\ym\downloadprocess.php:13) in C:\Program Files\xampp\htdocs\ym\downloadprocess.php on line 27
  2.  
i dont understand ,what is the problem...
please help me...i m not familiar with php....
Thank u
May 18 '08 #1
Share this Question
Share on Google+
10 Replies


P: 10
hi it's me again..
i try to use your coding...
all of it...
i've got problem with the coding on phase 4...

whenever i want to download,,,
it will give me a warning...similar to the previous warning..

Expand|Select|Wrap|Line Numbers
  1. Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\second\get_file.php:13) in C:\Program Files\xampp\htdocs\second\get_file.php on line 69
  2.  
but below the warning, it display the file content
but with all this ����)��� mixed up with the original content..


can u tell me what i did wrong?
please....
May 18 '08 #2

Atli
Expert 5K+
P: 5,058
Hi. Welcome to Bytes!

This error is shown when you try to modify the header information after you have already added content to the response.

To use the header function, nothing can be added to the response before it is called. Not even a single white-space. After anything is added, the headers are locked.

Check out line 13 in your downloadprocess.php and see what's there. The error indicates that this is where you started adding output.

P.S. I've moved your question into it's own thread in the PHP Forums, so not to crowd the article's comment thread.
May 19 '08 #3

TheServant
Expert 100+
P: 1,168
Maybe a silly question, but does your connectdb.php have any output (echo/print)? If it does, you will have sent info before finished modifying the headers and get that error.
May 19 '08 #4

P: 8
Any headers has got to be placed before any HTML is output to the screen.

I used to get that problem but if you place any processing php about the <html> tag, it should work.

There is a workaround using ob_start() and ob_flush() and the start and end of your document which will get rid of the errors
May 19 '08 #5

P: 10
hi ....
thank u very much for the advice....
my coding's problem is solved....
thank u so much....
all of u...
i love u guys so much
^_^
won't be able to solve my prob without u guys.....

thank u.....
next time when i got problem with coding...can i ask u guys again?
^_^ thank u.....
May 19 '08 #6

Markus
Expert 5K+
P: 6,050
hi ....
thank u very much for the advice....
my coding's problem is solved....
thank u so much....
all of u...
i love u guys so much
^_^
won't be able to solve my prob without u guys.....

thank u.....
next time when i got problem with coding...can i ask u guys again?
^_^ thank u.....
We love you too..

I think.

You're welcome back anytime!
May 19 '08 #7

TheServant
Expert 100+
P: 1,168
hi ....
thank u very much for the advice....
my coding's problem is solved....
thank u so much....
all of u...
i love u guys so much
^_^
won't be able to solve my prob without u guys.....

thank u.....
next time when i got problem with coding...can i ask u guys again?
^_^ thank u.....
Can you tell us how you solved it so others can learn as well. Thanks!
May 19 '08 #8

P: 10
hehe
simple...
there a html code above my php coding,,,
i just switch the php coding above the html coding...
then it's done....
i guess i m not alert with the coding
thats y i have a hard time to detect my coding mistakes
May 19 '08 #9

TheServant
Expert 100+
P: 1,168
hehe
simple...
there a html code above my php coding,,,
i just switch the php coding above the html coding...
then it's done....
i guess i m not alert with the coding
thats y i have a hard time to detect my coding mistakes
That's why it's good to have some friends look over the code. I know I have spent a very long time on that problem too, tryingt o find where I have used some html! Anyway, thanks for dropping by and we'll see you next time.
May 19 '08 #10

ronverdonk
Expert 2.5K+
P: 4,258
This 'headers not sent problem' pops up every 2 months or so, you can almost set your watch on it. And the answer is always the same: you send data to your browser (mostly unintentionally) before sending any other output. So you are getting this message is because you have may have/be:
  • Whitespace before the opening php tag <?php
  • Outputting something to the browser before you use session_start, header, setcookie etc
session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\test.php:6) in C:\server\www\test.php on line 8
Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
So lets look at test.php:
Expand|Select|Wrap|Line Numbers
  1.  1 <html>
  2.  2 <head>
  3.  3 <title>Header test</title>
  4.  4 </head>
  5.  5 <body>
  6.  6 <?php
  7.  7
  8.  8 header("Location: http://www.google.com");
  9.  9
  10. 10 ?>
  11. 11 </body>
  12. 12 </html>
As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.[/quote]Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

Ronald
May 19 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.