Error using Header

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Error using Header

Post by ljCharlie »

I have the following code of updating MySQL database and upload a file at the same time.

Code: Select all

<?php 
if (isset($_FILES['flNewFile'])) {
	$KW_max_size=500000000;
	$extension="";
    if (is_uploaded_file($_FILES['flNewFile']['tmp_name'])) { 
        if($_FILES['flNewFile']['size'] <= $KW_max_size) { 
            $realname = $_FILES['flNewFile']['name']; 
			$ext_array =explode(".",$realname);
			$last_position = count($ext_array) - 1; 
			$extension = $ext_array[$last_position];
			$extAllowed=array ('pdf','doc');
			$ii=count($extAllowed); 
			$flag=0;
			for($i=0;$i<$ii;$i++){ 
				if ($extAllowed[$i]==$extension)
					$flag=1;
			}
			if ($extAllowed[0]=="all")
				$flag=1;
            if(copy($_FILES['flNewFile']['tmp_name'], "minutes/".$realname) && $flag==1) { 
            	Header("Location: "); 
            	 } else Header("Location: mtngMinutes_failedFileSize.shtml");
        } else Header("Location: mtngMinutes_failedOther.shtml");
    } else Header("Location: mtngMinutes_failedFileExtn.shtml");
}
?>

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmInsertMtngMinutes")) {
  $insertSQL = sprintf("INSERT INTO minutes (name, descrpt, file, location, mntDate) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['fldName'], "text"),                       
					   GetSQLValueString($_POST['fldDescrpt'], "text"),
					   GetSQLValueString($realname, "text"),
                       GetSQLValueString($_POST['hdnLocation'], "text"),
                       GetSQLValueString($_POST['hdnDate'], "date"));

  mysql_select_db($database_lopha, $lopha);
  $Result1 = mysql_query($insertSQL, $lopha) or die(mysql_error());

  $insertGoTo = "mtngMinutes_insert_success.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_lopha, $lopha);
$query_rs_mtngMinutesInsert = "SELECT * FROM minutes";
$rs_mtngMinutesInsert = mysql_query($query_rs_mtngMinutesInsert, $lopha) or die(mysql_error());
$row_rs_mtngMinutesInsert = mysql_fetch_assoc($rs_mtngMinutesInsert);
$totalRows_rs_mtngMinutesInsert = mysql_num_rows($rs_mtngMinutesInsert);
?>
I recieved this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/lopha/public_html/Newface/mtngMinutes_insert.php:29) in /home/lopha/public_html/Newface/mtngMinutes_insert.php on line 76

The line 76 is this line,

Code: Select all

header(sprintf("Location: %s", $insertGoTo));
on the code above. I'm guessing that it has to do with this lines of code I have earlier on the page.

Code: Select all

if(copy($_FILES['flNewFile']['tmp_name'], "minutes/".$realname) && $flag==1) { 
            	Header("Location: "); 
            	 } else Header("Location: mtngMinutes_failedFileSize.shtml");
        } else Header("Location: mtngMinutes_failedOther.shtml");
    } else Header("Location: mtngMinutes_failedFileExtn.shtml");
Any help is greatly appreciated. I have been working this error for sometime now and couldn't figure out why.

ljCharlie
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

notice how it says php says: output started at /home/lopha/public_html/Newface/mtngMinutes_insert.php:29 ? That means line 29 of mtngMinutes_insert.php started the output.

If you remove lines 26 through 28 (here) it should clear up the warning.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Wow! Never thought would solve the problem. All this time I thought it had something to do with using to many times of the Header(). Can you explain to me why this solve the problem so next time I don't make the same mistake?

Many thanks for the help.

ljCharlie
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

viewtopic.php?t=1157

Seems no one searches anymore :cry:
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thanks! I now understand why.

ljCharlie
Post Reply