4 Replies - 502 Views - Last Post: 02 July 2012 - 05:32 PM Rate Topic: -----

#1 drpepperman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-July 12

Warning: Cannot modify header information

Posted 02 July 2012 - 04:14 PM

I'm new to php/sql and tried the dream in code tutorial 'Uploading Images With PHP' everything seems to work but I get the following error:

'Warning: Cannot modify header information - headers already sent by (output started at /public_html/test/includes/conn.php:10) in /public_html/test/upload.php on line 81'

anyone know how to remedy this? Thank you!!!

// Lets attempt to move the file from its temporary directory to its new home
75
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
76
{
77
    // NOTE: This is where a lot of people make mistakes.
78
    // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
79
    $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";
80
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
81
    header("Location: images.php");
82
    exit;
83
}
84
else
85
{
86
    // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
87
    // Make sure you chmod the directory to be writeable
88
    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
89
    header("Location: index.php");
90
    exit;
91
}
92
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Warning: Cannot modify header information

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Warning: Cannot modify header information

Posted 02 July 2012 - 04:16 PM

If you have output ANYTHING before the header() call, including a single whitespace, then this is what happens.

Post all your code so we can show you what you did wrong.
Was This Post Helpful? 0
  • +
  • -

#3 drpepperman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-July 12

Re: Warning: Cannot modify header information

Posted 02 July 2012 - 04:18 PM

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

	if (in_array($file['type'], $valid_types))
		return 1;
	return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$image = $_FILES['image'];

// Sanitize our inputs
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $fname == "" || $lname == "" || $image['name'] == "" )
{
	$_SESSION['error'] = "All fields are required";
	header("Location: index.php");
	exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
	$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
	header("Location: index.php");
	exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	$_SESSION['error'] = "A file with that name already exists";
	header("Location: index.php");
	exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
	// NOTE: This is where a lot of people make mistakes.
	// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
	$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";
	$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
	header("Location: images.php");
	exit;
}
else
{
	// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	// Make sure you chmod the directory to be writeable
	$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
	header("Location: index.php");
	exit;
}
?>


Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Warning: Cannot modify header information

Posted 02 July 2012 - 04:31 PM

Take a closer look at the error message. It tells you exactly where the problem is.

Quote

Warning: Cannot modify header information - headers already sent by (output started at /public_html/test/includes/conn.php:10) in /public_html/test/upload.php on line 81

Something on line #10 in your conn.php file is sending output, which in turn forces PHP to send the HTTP headers so it can start sending said output, which makes it impossible for you to alter the headers later in the code with the header function.

Like JackOfAllTrades says, nothing must be printed, or otherwise added to the output stream, if you intend to alter the headers. - If that is impossible (which is rare, if you arrange things properly) you can use the Output Buffering functions to force PHP to hold off on sending output before you are ready.
Was This Post Helpful? 0
  • +
  • -

#5 drpepperman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-July 12

Re: Warning: Cannot modify header information

Posted 02 July 2012 - 05:32 PM

Thank you! Very much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1