I have the following code of updating MySQL database and upload a file at the same time.
<?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,
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.
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