?
Solved

Would someone tell me why i am getting this error(Warning: Cannot modify header information - headers already sent by...

Posted on 2005-03-12
14
Medium Priority
?
276 Views
Last Modified: 2008-02-01
Hello all experts,
I am an ASP developer, and i was asked to complete a php project that have few errors .
I hope that u can help me in solving this errors with me.
Here is the first one:
THe site has a CMS area for editing the contents of the site and uploading images.When i click on a button to browse the image and find it then i click the submit button on that page i get this error:

Created thumb ../upload_thumb/96678COR.jpg

Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/desertdreamslandscaping.com/httpdocs/admin/inc/functions.php:82) in /home/httpd/vhosts/desertdreamslandscaping.com/httpdocs/admin/actions/add.images.php on line 31

Would you tell me what do this means as i am not familiar with PHP errors.
Thanks,
Melfeky
0
Comment
Question by:Melfeky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 600 total points
ID: 13524090
>> Warning: Cannot modify header information - headers already sent by

header( http://www.php.net/header ) function redirects to another page. It should be called before any output is sent to the browser. There should not be any sort of echo statements or HTML tags before the header function line in your code.

Not even a single whitespace should be present before the php starting tag as it is also sent to the browser. I mean :

<?php   // Correct.

  <?php    // Wrong, see the white space before the starting tag.

Same rule applies for any included files in the code.

If you by anway want to use echo statements used before the header function, you can use output buffering ( http://www.php.net/ob_start ). Though I'd say whatever you want to display before header function is called, have it in a PHP variable and display it after the header function line.

// Don't forget to call exit immediately next to the header function

// If you have any session data, you have to write and close the session data first before calling the header function. Like :

<?php
session_start();

// your code ..

session_write_close();
header("Location: http://www.domain.com");
exit();
?>

0
 

Author Comment

by:Melfeky
ID: 13524386
hello,
i see no space in this tag<?php //,

Here is the code of the page that i want to upload the image in:
<?
      include("inc/functions.php");
      $filename=$HTTP_POST_FILES['imgpath']['name'];
      $ftype=($HTTP_POST_FILES['imgpath']['type']);
      $uploaddir = "upload_img/";
      $uploadfile = $uploaddir.$filename;
      if ($ftype == 'image/gif' or $ftype =='image/jpeg' or $ftype == 'image/pjpeg'){
      move_uploaded_file($_FILES['imgpath']['tmp_name'], $uploadfile);
      $img_path = $uploaddir;
    $img_name = $filename;
    $thumb_path ="upload_thumb/";
    $size = 250;
    mkthumb($img_path, $img_name, $thumb_path, $size);
      }else { $err = "true";}
      
      header("location:../add.services.php");
      
?>



This is the add.services.php code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Desert Dreams</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
      margin-top: 0px;
      margin-left: 0px;
      margin-right: 0px;
      margin-bottom: 0px;
}
-->
</style>
<link href="../css/global.css" rel="stylesheet" type="text/css">

</head>

<body>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="top"><table width="765"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="left" valign="top"><? include("inc/header.php"); ?></td>
      </tr>
    </table>
    <table width="765" border="0" cellspacing="0" cellpadding="0">
        <tr align="left" valign="top">
          <td width="115" align="center" bgcolor="#88A3B9">&nbsp;</td>
          <td width="650"><table width="95%"  border="0" align="center" cellpadding="0" cellspacing="0">
            <tr align="left" valign="top">
              <td width="100%">&nbsp;</td>
            </tr>
            <tr align="left" valign="top">
              <td><table width="84%"  border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td colspan="2" align="center" valign="middle">&nbsp;</td>
                </tr><? if($success=="true") { ?>
                <tr>
                  <td colspan="2" align="center" valign="middle" class="text">Content has been Successfully Entered.</td>
                </tr><? } if($success=="false") { ?>
                <tr>
                  <td colspan="2" align="center" valign="middle" class="text">Invalid File Extension.</td>
                </tr><? } ?>
                <tr>
                  <td colspan="2" align="left" valign="middle" class="toplinks">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="2" align="left" valign="middle" class="new_headings">Add Content in Services</td>
                </tr>
                <tr>
                  <td colspan="2" align="left" valign="middle" class="text">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="2" align="left" valign="middle" class="text"><form action="actions/add.images.php" method="post" enctype="multipart/form-data" name="form1">
                      <table width="100%"  border="0" cellspacing="2" cellpadding="2">
                        <tr>
                          <td align="center" valign="top">Select Category </td>
                          <td align="left" valign="middle"><select name="cat" id="cat">
                            <option value="Xeriscape">Xeriscape</option>
                            <option value="Tropical">Tropical</option>
                            <option value="Theme based">Theme based</option>
                            <option value="Others">Others</option>
                          </select></td>
                        </tr>
                        <tr>
                          <td align="center" valign="top">Image Path </td>
                          <td align="left" valign="middle"><input name="imgpath" type="file" id="imgpath"></td>
                        </tr>
                        <tr>
                          <td align="center" valign="top">Description </td>
                          <td align="center" valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                          <td colspan="2" align="center" valign="top"><textarea name="txtarea" cols="65" rows="10" >
              </textarea>
                             
                              </td>
                        </tr>
                        <tr>
                          <input type="hidden" name="goto" value="news">
                          <td colspan="2" align="center" valign="top"><input type="submit" name="Submit" value="Submit"></td>
                        </tr>
                      </table>
                  </form></td>
                </tr>
                <tr>
                  <td colspan="2" align="left" valign="middle" class="text">&nbsp;</td>
                </tr>
              </table></td>
            </tr>
          </table></td>
        </tr>
      </table>
      <table width="765" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="25" align="center" valign="middle" bgcolor="#000000" class="news">Copyrights protected by <a href="#">desertdreams.com</a> 2004. </td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>



This is the functions.php code:
<?
function mkthumb($dirname, $filename, $thumbdir, $thumb_size = 120)
{
    if (isset($filename))
    {
        //get filetype and check to make sure it is an accepable image. if gif, jpeg, or png create
        //source image.
        $imtype = exif_imagetype($dirname.$filename);
     
        switch($imtype)
        {
            case 1 : //Image is type gif.
                $im = imagecreatefromgif($dirname.$filename);
                break;
            case 2 : //Image is type jpeg.
                $im = imagecreatefromjpeg($dirname.$filename);
                break;
            case 3 : //Image is png.
                $im = imagecreatefrompng($dirname.$filename);
                break;
            default : //File not accepted.
                echo "Error: $filename is an invalid file.<br>";
                break;
        }
     
        if (isset($im))  
        {
            //get width of source photo.
            $width = ImageSX($im);
            $height = ImageSY($im);
             
            //set aspect ratio so that the thumb is not distorted.
            if ($width < $height)
            {
                $asp_ratio = $thumb_size/$height;
            }
            else
            {
                $asp_ratio = $thumb_size/$width;
            }

            //calculate thumb width using aspect ratio percetage.
            $thumb_width = round(($width * $asp_ratio));
            $thumb_height = round(($height * $asp_ratio));
             
            //create thumb image.
            $im_thumb = imagecreatetruecolor($thumb_width, $thumb_height);
             
            //resize and copy source image to the thumb image.
            imagecopyresampled($im_thumb, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width,$height);
             
            //create
            switch($imtype)
            {
                case 1 : //Image is type gif.
                    $im_new = imagegif($im_thumb,$thumbdir.$filename);
                    break;
                case 2 : //Image is type jpeg.
                    $im_new = imagejpeg($im_thumb,$thumbdir.$filename);
                    break;
                case 3 : //Image is png.
                    $im_new = imagepng($im_thumb,$thumbdir.$filename);
                    break;
                default :
                    $im_new = imagejpeg($im_thumb,$thumbdir.$filename);
                    break;
            }    
            imagedestroy($im);
            imagedestroy($im_thumb);
        }
        else
        {
            echo "Error: Cannot process image<br>";
            return false;
        }
    }
    else
    {
        echo "Error: Filename required<br>";
        return false;
    }
    echo "Created thumb $thumbdir$filename<br>";
    return true;
}

?>
0
 
LVL 16

Accepted Solution

by:
sam85281 earned 800 total points
ID: 13524711
This is a longshot, but recently I developed an application alot like the one you're doing now for a client.

I got header errors over and over, no matter what I tried it wouldn't fix it!

Turned out that having the permissions set to 777 was the problem.  

Set the permissions to 755 and it worked fine.

If that sounds like it doesn't make sense...I agree.  I've done hundreds of similar applications chmoding to 77 and it worked fine.  But on this particular host it would not work that way????

Worth a shot.

-Sam
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

Author Comment

by:Melfeky
ID: 13524763
i am sorry but i am not familiar with the 777 nor 755 permission issue.
where shall i change this ?
thanks
0
 
LVL 16

Expert Comment

by:sam85281
ID: 13524891
Ahhhh, that depends.  What are you using to upload files?  Cpanel - WS_FTP - Another ftp program - etc. ?  It's different for every method.

-Sam
0
 

Author Comment

by:Melfeky
ID: 13525230
I realy don't know,
the previous coder left me without any excuses and i couldn't contact him and this is my first time to open any PHP file.
I will tell you what is happening,
The previous coder has created a simple CMS into my website, where the admin can upload images into the pages and edit the text too.
When i opened the page and clicked on an image to upload it,and clicked the submit button i got the above error.
Could you tell from this what is the problem.
Thanks
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13525234
you have "echo" statements in functions.php

I already told the condition of no output sending to browser is applicable to included file also. Its allowed to have die() statement but not echo.

If by anyway you want to use the echo statement in the function, put ob_start(); at the top of your and ob_en_flush() at the bottom after header statement.

Making chmod ( www.php.net/chmod ) is very easy and you can do it in the program itself. See the link.
0
 

Author Comment

by:Melfeky
ID: 13526025
i still don't get it, where shall i put " put ob_start(); at the top of your and ob_en_flush() at the bottom after header statement" in which file do u mean.
would you put it in my code and paste it here ,so i will understand what do u mean.
I am just a very very newbie :)


0
 

Expert Comment

by:1020
ID: 13526073
what i know (without reading what other said)  is that this error  can occur also when  ending your php file

?> dont put anything after it , nothing @ all not even a /n , an empty line or something
it should be the end ..

i dont know if this is your case ...
0
 

Assisted Solution

by:1020
1020 earned 600 total points
ID: 13526087
in your case delete this :


}

?>                          <<-- end of php code
                               <<----  delete this white space
 ------------------------------- <<-- let say this is the end
0
 

Author Comment

by:Melfeky
ID: 13526135
I have deleted the white space but still the same error.
0
 

Expert Comment

by:1020
ID: 13526969
in all the php files that use  header thing ?
0
 
LVL 16

Expert Comment

by:sam85281
ID: 13527357
You need to set the permissions right on all the scripts and folders.  I've had the similar problem several times.  On most servers I can set permissions to 777 and everything works fine.  But, on a few, it wouldn't work unless the permissions were set to 755 and I got the exact same errors and tried everything above with no luck.

What are you using to edit/save/upload your files.  How are you accessing them?  Is it an online file manager?  Or what?

Give me a clue about that and I can tell you how to set the permissions.

-Sam
0
 

Author Comment

by:Melfeky
ID: 13530018
I can give you my account information to the server so u can check this by yourself as i don't know anything about PHP .

this is my yahoo id:coderpark and my msn is n_yehia@msn.com

Thanks
0

Featured Post

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question