473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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
10 2338
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
5,058 Expert 4TB
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
1,168 Expert 1GB
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
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
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
6,050 Expert 4TB
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
1,168 Expert 1GB
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
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
1,168 Expert 1GB
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
4,258 Expert 4TB
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

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

Similar topics

2
by: Dariusz | last post by:
I have a problem where when I run the PHP code offline, there are no errors produced and the code runs as expected. However when I uploaded the same script and run it, it says the headers have...
4
by: Leslie Houk | last post by:
I'm just learning PHP, and I'm having a problem using fpassthru(). I want to download a file using the following code segment I found, which I put into my testing file called portal.php: ...
1
by: mr_burns | last post by:
hi, i have been tryin to prompt the download of a file on the server using the following code: // send headers, forcing download: header("Content-type: application/word");...
11
by: Francisco Mendez | last post by:
I get the following message when trying to run my script: "Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/newcustomer.php:23) in...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
3
by: mail1_fr | last post by:
Hi, I have 2 questions : 1 - How to launch a sheet S2 from a sheet S1 ? ------------------------------------------------ We have to is the header() function or do there is something else ?...
0
by: JRough | last post by:
I have the following code at the end of a page that lists some query results. It starts in a form that asks the user for input, one of two template pages. One template is a form for user input if...
3
by: siikworm | last post by:
Hi, I'm having a couple of (possibly related) problems. 1) When I run this code as-is, I get an extra output to the screen...usually three numbers such as "559" or "205". The output appears in...
11
by: ririe | last post by:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Inovasi\get_file.php:2) in C:\xampp\htdocs\Inovasi\get_file.php on line 40 Warning: Cannot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.